Reputation: 125
I have a data frame like this
a b f
0 S NA
0 A 0.88
1 A 0.85
0 A 0.98
1 A 1.01
1 S NA
1 B 1.03
and would like to add NA to column 'f', based on the value of 'a' (if a == 1)
; also not affecting the NAs that are already in the column'f'
so it would look like this
a b f
1: 0 S NA
2: 0 A 0.88
3: 1 A NA
4: 0 A 0.98
5: 1 A NA
6: 1 S NA
7: 1 B NA
any suggestions is greatly appreciated Thanks m
Upvotes: 0
Views: 53
Reputation: 99331
You can also use the is.na<-
replacement function.
is.na(df$f) <- df$a == 1
Here's an example with your data.
within(df, is.na(f) <- a == 1)
# a b f
# 1 0 S NA
# 2 0 A 0.88
# 3 1 A NA
# 4 0 A 0.98
# 5 1 A NA
# 6 1 S NA
# 7 1 B NA
Upvotes: 1