TDP
TDP

Reputation: 374

Creating indicator variable using multiple logical statements

I am new to R and to programming in general. I would like to create indicator variables based on multiple nested conditions. My data is looks like this:

id   city   income
 1    A      100
 2    A      65
 3    B      110
 4    B      80
 5    C      60

I would like to to write a script that does something like this:

        if (mydata$city=="A" & mydata$income >= 90) {
            mydata$x <- 1 
    }       else if (mydata$city=="B" & mydata$income >= 100) {
            mydata$x <- 1
    }       else {
            mydata$x <- 0
}

I would like the end result to be this

id   city  income  x
 1    A     100    1
 2    A     65     0
 3    B     110    1
 4    B     80     0
 5    C     60     0

Thank you in advance for any advice!

Upvotes: 0

Views: 397

Answers (3)

akrun
akrun

Reputation: 887098

We can use data.table

library(data.table)
setDT(df1)[, x := as.integer(city =="A" & income >= 90 | city == "B" & income >=100)]
#   id city income x
#1:  1    A    100 1
#2:  2    A     65 0
#3:  3    B    110 1
#4:  4    B     80 0
#5:  5    C     60 0

Or with base R

df1$x <- with(df1, as.integer(city =="A" & income >= 90 | city == "B" & income >=100))

Upvotes: 0

thepule
thepule

Reputation: 1751

Define your function:

myfun <- function(x,y) {
        if(x == "A" & y >= 90) {
                1
        } else if(x == "B" & y >= 100) {
                1
        } else 0
}

Use mapply:

mydata$x <- mapply(myfun, mydata$city, mydata$income)

Upvotes: 1

akuiper
akuiper

Reputation: 214957

dplyr::mutate(myData, x = ifelse((city == "A" & income >= 90) | (city == "B" & income >= 100), 1, 0))

  id city income x
1  1    A    100 1
2  2    A     65 0
3  3    B    110 1
4  4    B     80 0
5  5    C     60 0

Upvotes: 0

Related Questions