我有一个数据框架- df -与基因组数据。最后一首歌有两个字母变体。
id crm pos allele
160841 rs2237282 11 1273948 AG
160842 rs6417577 11 1276796 AC
165677 rs2151342 11 1199626 GT
165678 rs2749240 11 1258025 AG我想把最后一封信分成两份,一份一份。
id crm pos allele allele2
160841 rs2237282 11 1273948 A G
160842 rs6417577 11 1276796 A C
165677 rs2151342 11 1199626 G T
165678 rs2749240 11 1258025 A G我在RStudio 1.1.419,R3.4.3中尝试了使用dplyr和tidyr,但没有成功:
我怎样才能得到想要的分裂呢?
发布于 2018-02-05 23:28:05
在separate中,sep参数可以是数字参数,并表示要拆分的字符位置,因此:
separate(df, allele, into = c("allele1", "allele2"), sep = 1)给予:
id crm pos allele1 allele2
160841 rs2237282 11 1273948 A G
160842 rs6417577 11 1276796 A C
165677 rs2151342 11 1199626 G T
165678 rs2749240 11 1258025 A G发布于 2018-02-05 23:26:09
使用基数r:
HERE=data.frame(A1=character(),A2=character())
cbind(data,strcapture("(.)(.)",data$allele,HERE))
id crm pos allele A1 A2
160841 rs2237282 11 1273948 AG A G
160842 rs6417577 11 1276796 AC A C
165677 rs2151342 11 1199626 GT G T
165678 rs2749240 11 1258025 AG A G发布于 2018-02-06 02:21:15
除了separate之外,extract是提尔包的另一个选项。这可以通过在regex参数中指定捕获组来实现。
library(tidyr)
df %>%
extract(allele, into = c("allele1", "allele2"), regex = "([ATCG])([ATCG])")
# id crm pos allele1 allele2
# 160841 rs2237282 11 1273948 A G
# 160842 rs6417577 11 1276796 A C
# 165677 rs2151342 11 1199626 G T
# 165678 rs2749240 11 1258025 A Ghttps://stackoverflow.com/questions/48633088
复制相似问题