Home > other >  creating a new variable using ifelse or else if
creating a new variable using ifelse or else if

Time:07-28

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)

CodePudding user response:

I think you're looking for case_when and c_across:

library(dplyr)
dat1 %>%
  rowwise() %>%
  mutate(fam_type = case_when(
    all(1:4 %in% c_across(a:f)) ~ "ext", 
    all(1:2 %in% c_across(a:f)) ~ "nuc", 
    all(c(1, 5) %in% c_across(a:f)) ~ "grand", 
    all(c(1, 3) %in% c_across(a:f)) ~ "single", 
    TRUE ~ "other")
  ) %>%
  ungroup()
# # A tibble: 6 x 8
#   id        a     b     c     d     e     f fam_type
#   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>   
# 1 aaa       1     2     3    NA    NA    NA nuc     
# 2 bbb       1    NA     2    NA    NA    NA nuc     
# 3 ccc       2    NA     1    NA    NA    NA nuc     
# 4 ddd      NA     2     3     4    NA     1 ext     
# 5 eee       1    NA    NA    NA     5    NA grand   
# 6 fff       1    NA    NA    NA    NA    NA other   
  • Related