gpt4 book ai didi

r - 将一列包含确切信息的列拆分为两列

转载 作者:行者123 更新时间:2023-12-04 13:14:30 26 4
gpt4 key购买 nike

我的数据看起来像这样:

    SNP Geno Allele
marker1 G1 AA
marker2 G1 TT
marker3 G1 TT
marker1 G2 CC
marker2 G2 AA
marker3 G2 TT
marker1 G3 GG
marker2 G3 AA
marker3 G3 TT

我希望它看起来像这样:
    SNP Geno Allele1 Allele2
marker1 G1 A A
marker2 G1 T T
marker3 G1 T T
marker1 G2 C C
marker2 G2 A A
marker3 G2 T T
marker1 G3 G G
marker2 G3 A A
marker3 G3 T T

我正在使用这个:
strsplit(Allele, split extended = TRUE)

但这是行不通的。我是否需要其他命令?

最佳答案

从头到尾的另一种方法:

制作可复制的数据:

dat <- read.table(header = TRUE,  text = "SNP Geno    Allele
marker1 G1 AA
marker2 G1 TT
marker3 G1 TT
marker1 G2 CC
marker2 G2 AA
marker3 G2 TT
marker1 G3 GG
marker2 G3 AA
marker3 G3 TT")

更新后的提取“等位基因”列,将其拆分为单个字符,然后将这些字符分成数据框的两列:

EITHER
dat1 <- data.frame(t(matrix(
unlist(strsplit(as.vector(dat$Allele), split = "")),
ncol = length(dat$Allele), nrow = 2)))

@joran的建议后,
dat1 <- data.frame(do.call(rbind, strsplit(as.vector(dat$Allele), split = "")))

THEN

将列名称添加到新列:
names(dat1) <- c("Allele1", "Allele2")

将两个新列附加到原始数据表中的列,如@ user1317221所示:
dat3 <- cbind(dat$SNP, dat$Geno, dat1)
dat$SNP dat$Geno Allele1 Allele2
1 marker1 G1 A A
2 marker2 G1 T T
3 marker3 G1 T T
4 marker1 G2 C C
5 marker2 G2 A A
6 marker3 G2 T T
7 marker1 G3 G G
8 marker2 G3 A A
9 marker3 G3 T T

关于r - 将一列包含确切信息的列拆分为两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10421668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com