I have a data set with many variables, two of which called "animal" and "plant". Both variable are factors, and both are binary, i.e. they are either a text value, or NA.
For example:
animal <- c(NA, NA, "cat", "cat", NA)
plant <- c("ivy", NA, "ivy", NA, NA)
value <- c(1:5)
df <- data.frame(animal, plant, value)
> df
animal plant value
1 <NA> ivy 1
2 <NA> <NA> 2
3 cat ivy 3
4 cat <NA> 4
5 <NA> <NA> 5
When the value of plant is "ivy" and the value of animal is "cat", I want to change the value of plant to NA (i,e, the two things can not be true and the animal value takes priority. I don't any changes in my other variables
I've tried the following but get an error message:
df <- df %>% if (isTRUE(animal == "cat")) {plant==NA}
Error in if (.) isTRUE(animal == "cat") else { :
argument is not interpretable as logical
In addition: Warning message:
In if (.) isTRUE(animal == "cat") else { :
the condition has length > 1 and only the first element will be used
My goal output is:
> df
animal plant value
1 <NA> ivy 1
2 <NA> <NA> 2
3 cat <NA> 3
4 cat <NA> 4
5 <NA> <NA> 5
I would really appreciate any help. I'm sure there is a really simple way of doing this, maybe I can't see the wood for the trees.
Aucun commentaire:
Enregistrer un commentaire