Home > Software engineering >  R: categorize obtain a new table
R: categorize obtain a new table

Time:09-05

enter image description here

I have such a table with 3 colomns rs10330, rs18976 and rs749. I want to obtain the last colomn identifiedID which row have one of AG or GG, GT or TT, AT or TT in each colomn,the identifiedID will be 1, and which have two the identifiedID will be 2. As for row 1 ,there is no AG or GG, GT or TT, AT or TT for 3 colomn,therefore ,the identifiedID is 0.In order to obtain identifiedID, what is the code?


Data

datmp <- data.frame(rs10330=c('AA','AG','GG','AG','AA'), 
                    rs18976=c('GG','GT','GT','GG','GG'), 
                    rs7498=c( 'AA','AT','TT','AT','TT')) 
identifiedID <- c(0,3,3,2,1) 
datmp2 <- data.frame(datmp,identifiedID)

CodePudding user response:

A very verbose solution would be

datmp %>%
    mutate(identifiedID = rs10330 %in% c("AG", "GG")   
               rs18976 %in% c("GT", "TT")   
                    rs7498 %in% c("AT", "TT"))
#  rs10330 rs18976 rs7498 identifiedID
#1      AA      GG     AA            0
#2      AG      GT     AT            3
#3      GG      GT     TT            3
#4      AG      GG     AT            2
#5      AA      GG     TT            1
  •  Tags:  
  • r
  • Related