简体   繁体   English

R:分类获取新表

[英]R: categorize obtain a new table

在此处输入图像描述

I have such a table with 3 colomns rs10330, rs18976 and rs749.我有这样一张表,有 3 个列 rs10330、rs18976 和 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?我想获取最后一列identifiedID,哪一行在每列中有AG或GG,GT或TT,AT或TT之一,identifiedID将为1,并且有两个identifiedID将为2。至于第1行,有3列没有AG或GG,GT或TT,AT或TT,因此,identifiedID为0。为了获得identifiedID,代码是什么?


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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