I know how to conditionally replace levels of a variable using dplyr/tidyr. Here's some toy data (the real dataset is much larger and more complex):
dat <- data.frame(animal=c("cat", "cat", "dog", "cat"),
size=c("big", "big", "big", "small"))
newdata <- dat %>% mutate(newanimal=replace(animal, animal=='cat' & size=='big', "fatcat"))
And I keep getting "invalid factor level, NA generated" - why?! These are factor variables, the specific combination of 'cat' and 'big' exists in the dataframe. Why do I get this error?
dat %>% mutate(newanimal=replace(animal, animal=='cat' & size=='big', "fatcat"))- Mattfilterhere. After the substitution, you want the whole table or just the filtered columns? - Marco De Virgilisas_data_framedoesn't work like this. - M--as_data_frameis deprecated in favor ofas_tibble. But to create a data frame, calltibbleordata.framedirectly - camille