jeudi 20 février 2020

Create a vector according to multiple conditions contained in vectors

Imagine I have this data.frame :

a <- c("Boy","Girl","Dog","Cat","Chair","Table")
b <- c(1:6)
df <- data.frame(a,b)

I would like to create a third column groupings elements in the first column according to predefined vectors :

human <- c("Boy","Girl") 
pet <- c("Dog","Cat")
object <- c("Chair","Table")

With this wished result :

df$Type <- c("Human","Human","Animal","Animal","Object","Object")

I managed to get it right for 2 groups but I don't know how to include the third one :

df$Type1 <- ifelse(df$a==human,"Human","Animal")

Aucun commentaire:

Enregistrer un commentaire