I have a dataset with lots of numerical variables, and a character variables that says whether or not low values are suppressed for that observation. In observations where values aren't suppressed, I want to replace NAs 0s (just for specific variables), and I can't figure it out. This is my data:
suppressed var1 var2
none 2 6
none NA 6
none 3 7
none NA NA
full 2 6
full 3 6
none 3 NA
partial NA 6
none 2 7
none NA NA
What I want to do is change NA to 0 in Var 1, if Suppressed=none. I tried
df$Var1<-if (df$suppressed=='none'&is.na(df$Var1)) 0
else df$Var1
and got
Error in if (df$suppressed == "none" & is.na(df$Var1)) 0 else df$Var1 :
argument is of length zero
Is there something wrong with my if else statement, or is there another way to do this?
Here's the structure of my data:
structure(list(suppressed = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L, 2L), .Label = c("full", "none", "partial"), class = "factor"), var1 = c(2, NA, 3, NA, 2, 3, 3, NA, 2, NA), var2 = c(6, 6, 7, NA, 6, 6, NA, 6, 7, NA)), .Names = c("suppressed", "var1", "var2"), row.names = c(NA, -10L), class = "data.frame")