Reputation: 67
I have two columns (df$Z
and df$A
)
I basically want to say: if df$Z is less than 5, then fill df$A
with an NA
and if not then leave df$A
alone. I've tried these things but am not sure where I'm going wrong or what the error message means.
if(df$X<5){df$A <- NA}
Error:
In if (df$X < 5) { : the condition has length > 1 and only the first element will be used
I also tried to do something more like this.
for(i in dfX){
if(df$X<5){
df$A <- "NA"
}
}
Upvotes: 1
Views: 22413
Reputation: 7113
The vectorized form of the if
statement in R is the ifelse()
function:
df$A <- ifelse( df$X < 5, NA, df$A )
However, in this case I would also go with @mark-heckmann's solution.
And please note, that "NA"
is not the same as NA
.
Upvotes: 1
Reputation: 81683
A simple way is the "is.na<-"
function:
is.na(df$A) <- df$Z < 5
Upvotes: 1
Reputation: 11431
No if statement needed. That's the magic of vectorization.
df$A[df$Z < 5] <- NA
Upvotes: 6