简体   繁体   中英

R: categorize obtain a new table

在此处输入图像描述

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM