【问题标题】:Separate two letter string in 4th column在第 4 列中分隔两个字母字符串
【发布时间】:2018-07-15 22:19:17
【问题描述】:

我有一个数据框 - df - 包含基因组数据。最后的 col 有两个字母的变体。

               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、R 3.4.3 中使用 dplyr 和 tidyr 尝试过,但没有成功:

  • 分离(df, allele, into=c("allele", "allele2"))
  • 分离(df, allele, into=c("allele", "allele2"), sep="")
  • 分离(df, allele, into=c("allele", "allele2"), sep="\c")
  • separate(df, allele, into=c("allele", "allele2"), sep=".")
  • 分离(df, allele, into=c("allele", "allele2"), sep=.)
  • 分离(df, allele, into=c("allele", "allele2"), sep=\c)

我如何得到想要的拆分?

【问题讨论】:

    标签: r data-science tidyr


    【解决方案1】:

    使用基础 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
    

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        【解决方案3】:
        library(tidyverse)
        
        df %>%
            mutate(allele2 = substr(allele, 2, 2)) %>%
            mutate(allele = substr(allele, 1, 1))
        

        【讨论】:

          【解决方案4】:

          除了separateextract 包中的另一个选项。这可以通过在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       G
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-06
            • 2018-01-06
            • 2020-02-01
            • 2022-12-01
            • 2012-12-24
            • 1970-01-01
            相关资源
            最近更新 更多