I have a dataframe
P1 P2 P3_1 P3_2
1 3 1 30
2 NA NA NA
2 NA NA NA
2 NA NA NA
1 1 1 10
I'm trying to set all the NA's to be 0 instead of NA's if P1 = 2.
The code I have is
df$P2 <- ifelse(df$P1 == 2, df$P2, 0)
Which returns this
P1 P2 P3_1 P3_2
1 0 1 30
2 NA NA NA
2 NA NA NA
2 NA NA NA
1 0 1 10
It seems that P2 gets changed to 0 when P1 = 1 instead. I know I could just make it so all NA's are 0 instead, but this trend is specific for when the value is 2. Any other NA's I wouldn't want to change to 0.
Where did I go wrong with the ifelse()
syntax? Is there a better way to do this and include all three columns that I need?
CodePudding user response:
When we create a logical in ifelse
, the 'yes' condition should be 0 and 'no' would be 'df$P2'. Or without the ifelse
, it can be assigned directly as well
df$P2[with(df, P1 == 2 & is.na(P2))] <- 0
-output
> df
P1 P2 P3_1 P3_2
1 1 3 1 30
2 2 0 NA NA
3 2 0 NA NA
4 2 0 NA NA
5 1 1 1 10
data
df <- structure(list(P1 = c(1L, 2L, 2L, 2L, 1L), P2 = c(3, 0, 0, 0,
1), P3_1 = c(1L, NA, NA, NA, 1L), P3_2 = c(30L, NA, NA, NA, 10L
)), row.names = c(NA, -5L), class = "data.frame")