Duck
Duck

Reputation: 39595

Not correct syntax trying to create a new variable in R with ifelse

Hi everybody I am trying to create a new variable in a data frame in R but my code with ifelse() doesn't work. My data frame F has the next structure (I include dput() version in the last part):

    ID rama2
1   1     7
2   2    16
3   3     1
4   4     7
5   5    16
6   6    16
7   7     7
8   8     9
9   9     1
10 10     7
11 11    16
12 12     7
13 13    13
14 14    20
15 15     7
16 16     9
17 17     3
18 18     3
19 19     9
20 20    20
21 21    NA
22 22     4
23 23     0
24 24     0
25 25     0
26 26     0
27 27     0
28 28     0
29 29     0
30 30     0
31 31     0
32 32     0

I am trying to compute a new variable name rama_act1 considering this conditions: 1. If rama2 is equal to 1 rama_act1 is equal to 1. 2. If rama2 is equal to 0 rama_act1 is equal to NA. 3. If rama2 is equal to NA rama_act1 is equal to 0. 4. Values different of 1 (except 0) in rama2 also are equal to 0.

I write a code with ifelse considering these conditions but something is wrong:

z$rama_act1=ifelse(z$rama2==1,1,
                   ifelse(z$rama2==0,NA,0))

I don't know what is happening because this doesn't work with a value of rama2 that has NA. I got this result using last code:

    ID rama2 rama_act1
1   1     7         0
2   2    16         0
3   3     1         1
4   4     7         0
5   5    16         0
6   6    16         0
7   7     7         0
8   8     9         0
9   9     1         1
10 10     7         0
11 11    16         0
12 12     7         0
13 13    13         0
14 14    20         0
15 15     7         0
16 16     9         0
17 17     3         0
18 18     3         0
19 19     9         0
20 20    20         0
21 21    NA        NA
22 22     4         0
23 23     0        NA
24 24     0        NA
25 25     0        NA
26 26     0        NA
27 27     0        NA
28 28     0        NA
29 29     0        NA
30 30     0        NA
31 31     0        NA
32 32     0        NA

Where there is a mistake in row 21 and I don't know why my code doesn't consider it. I would like to get something like this:

    ID rama2 rama_act1
1   1     7         0
2   2    16         0
3   3     1         1
4   4     7         0
5   5    16         0
6   6    16         0
7   7     7         0
8   8     9         0
9   9     1         1
10 10     7         0
11 11    16         0
12 12     7         0
13 13    13         0
14 14    20         0
15 15     7         0
16 16     9         0
17 17     3         0
18 18     3         0
19 19     9         0
20 20    20         0
21 21    NA         0
22 22     4         0
23 23     0        NA
24 24     0        NA
25 25     0        NA
26 26     0        NA
27 27     0        NA
28 28     0        NA
29 29     0        NA
30 30     0        NA
31 31     0        NA
32 32     0        NA

I think my code is considering NAs but it fails. The dput() version of my data frame is the next:

structure(list(ID = 1:32, rama2 = c(7, 16, 1, 7, 16, 16, 7, 9, 
1, 7, 16, 7, 13, 20, 7, 9, 3, 3, 9, 20, NA, 4, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0)), .Names = c("ID", "rama2"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 101L, 500L, 34309L, 39982L, 40735L, 
41019L, 44463L, 44464L, 44465L, 44480L, 44481L, 44482L), class = "data.frame")

Thanks for your help in advance.

Upvotes: 0

Views: 116

Answers (2)

agstudy
agstudy

Reputation: 121568

Rephrasing:

  1. If rama2 is equal to 1 rama_act1 is equal to 1.
  2. If rama2 is equal to 0 rama_act1 is equal to NA.
  3. If rama2 is equal to NA rama_act1 is equal to 0.
  4. Values different of 1 (except 0) in rama2 also are equal to 0.

Maybe (this)

  transform(dat,rama_act1=ifelse(is.na(rama2), 0,
                                 ifelse(rama2==1,1,
                                        ifelse(rama2==0,NA,0))))

Upvotes: 2

Matthew Lundberg
Matthew Lundberg

Reputation: 42639

The problem is that NA is not comparable:

ifelse(1==NA, 0, 1)
## [1] NA

To fix, check for NA specifically:

z$rama_act1=ifelse(!is.na(z$rama2) & z$rama2==1,1,
               ifelse(z$rama2==0,NA,0))

Upvotes: 2

Related Questions