user9292
user9292

Reputation: 1145

Creating a new variable with if statement in R

I’ve the following situation In my dataset, there are two variables (x1 and x2).

mydata <- data.frame(x1=c("a","b","c","n","a","d","b","l","a","c","t","a","b","d","c","l","n","b"), x2=c(1,NA,5,2,NA,5,4,NA,2,NA,2,6,NA,6,NA,2,6,NA))
mydata[is.na(mydata)] <- " "

    x1  x2
1   a   1
2   b    
3   c   5
4   n   2
5   a    
6   d   5
7   b   4
8   l    
9   a   2
10  c    
11  t   2
12  a   6
13  b    
14  d   6
15  c    
16  l   2
17  n   6
18  b   

I want to create a third variable, let’s call it x3, which is defined as

If (x1 = a or b or c AND x2= “ ”), then x3=1, else x3=0

The output is

    x1  x2  x3
1   a   1   0
2   b       1
3   c   5   0
4   n   2   0
5   a       1
6   d   5   0
7   b   4   0
8   l       0
9   a   2   0
10  c       1
11  t   2   0
12  a   6   0
13  b       1
14  d   6   0
15  c       1
16  l   2   0
17  n   6   0
18  b       1

Upvotes: 0

Views: 111

Answers (1)

akrun
akrun

Reputation: 887048

You can try

mydata$x3 <- (mydata$x1 %in% c('a', 'b', 'c') & mydata$x2==' ')+0
mydata$x3
#[1] 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1

Or as commented by @David Arenburg, other options to create a column include functions such as transform, within etc

transform(mydata, x3 = (x1 %in% letters[1:3] & x2 == ' ') + 0L)

Upvotes: 1

Related Questions