Imagine a vector animals <- c("dog", "wolf", "cat"); animals <- as.data.frame(animals)
I'd like to create a new vector that looks like this: animals$dogs <- c("dog", "dog", "cat)
Is there a dplyr
function that will perform this operation?
Imagine a vector animals <- c("dog", "wolf", "cat"); animals <- as.data.frame(animals)
I'd like to create a new vector that looks like this: animals$dogs <- c("dog", "dog", "cat)
Is there a dplyr
function that will perform this operation?
animals <- data.frame(animals=c("dog", "wolf", "cat"))
I believe the dplyr
idiom would be:
library("dplyr")
animals %>% mutate(dogs=ifelse(animals %in% c("dog","wolf"),
"dog",
"cat"))
You could also use car::recode()
for this.
library("car")
animals %>% mutate(dogs=recode(animals,"c('dog','wolf')='dog'"))
The fastest way I can think of is to subset the vector or data.frame and replace the "wolf" entries directly without ifelse
:
animals <- c("dog", "wolf", "cat")
dogs <- animals
dogs[dogs == "wolf"] <- "dog"
Or in case of a data.frame:
animals <- data.frame(animals=c("dog", "wolf", "cat"))
animals$dog <- animals$animals
animals$dog[animals$dog == "wolf"] <- "dog"
The advantage should be that you're only modifying a subset of the data instead of the whole vector. If your data is small, it probably won't make a difference or could even be slower than ifelse, but for a larger vector I believe it would perform better (not benchmarked, though).