With the following sample data I'm trying to create a new variable "category" based on the values of three columns variables (type, addict, and sex).
But I would like to combine type and addict into one group and sex in another group. So I use any to get logically to a set of logical vectors, is at least one of the values true or both of them true.
df <- data.frame(type = c("NA", "bad","NA"), addict=c('visky','wine','NA'),
sex=c('NA','male','NA'))
type addict sex
1 NA visky NA
2 bad wine male
3 NA NA NA
library(dplyr)
df%>%
mutate(category=ifelse(any(is.na(type)&addict=="visky")&any(is.na(sex)),"categ1",
ifelse(any(type=="bad"|addict=="wine")&any(!is.na(sex)),"categ2",
ifelse(any(is.na(type)&is.na(addict))&any(is.na(sex)),"categ3",NA))))
type addict sex category
1 NA visky NA categ2
2 bad wine male categ2
3 NA NA NA categ2
as it can be seen my ifelse loop is not working correctly. I cannot figured out why? Thx in advance
Aucun commentaire:
Enregistrer un commentaire