2022-07-25

creating a new variable using ifelse or else if

I have this data

id<-c("aaa","bbb","ccc","ddd","eee","fff")
a<-c(1,1,2,NA,1,1)
b<-c(2,NA,NA,2,NA,NA)
c<-c(3,2,1,3,NA,NA)
d<-c(NA,NA,NA,4,NA,NA)
e<-c(NA,NA,NA,NA,5,NA)
f<-c(NA,NA,NA,1,NA,NA)
dat1<-data.frame(id,a,b,c,d,e,f)

I wish to use conditions on the columns to produce this column.

The conditions are; if any row contains a combination of (1, 2, and 3) OR (1, 2) then its nuc

if any row contains a combination of 1,2,3,4 then its ext

if any row contains a combination of 1 and 5 then its grand

if it contains a combination of 1 and 3 only then its single

I would wish to have this column in the data frame

dat1$fam_type<-c(nuc,nuc,nuc,ext,gran,sing,other)


No comments:

Post a Comment