将矩阵/ Dataframe 的多列中的字符串拆分到新列中

dw1jzc5e  于 2023-03-20  发布在  其他
关注(0)|答案(4)|浏览(112)

如何拆分数据框/矩阵的多列并将结果保存为数据框?每个单元格有两个字符,但我只希望每个单元格有一个字符,其他字符对到下一个。
我尝试了这些方法,但分割完成后仍无法从结果中获取结果 Dataframe

geno_splitted_ls <- apply(mGenotype, 2, strsplit, split="") #split each column

do.call("as.data.frame", geno_splitted_ls) #collect results as dataframe fails
lapply(geno_splitted_ls, data.frame) #collect results as dataframe fails

数据如下所示

> dput(mGenotype)
structure(c("gg", "gg", "gg", "gg", "gt", "gg", "gg", "tg", "gg", 
"gg", "aa", "aa", "ac", "aa", "ca", "aa", "aa", "aa", "aa", "ac", 
"tt", "tt", "ct", "cc", "tt", "tt", "ct", "tc", "tc", "tt", "aa", 
"aa", "ag", "aa", "ga", "ga", "aa", "aa", "aa", "ag", "aa", "aa", 
"aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "tt", "tt", "tt", 
"tt", "tt", "gt", "tt", "tt", "tt", "tt"), dim = c(10L, 6L), dimnames = list(
    NULL, c("genotype1", "genotype2", "genotype3", "genotype4", 
    "genotype5", "genotype6")))
qxsslcnc

qxsslcnc1#

下面是一个基于tidyverse的解决方案:

library(tidyverse)

genotype <- structure(c("gg", "gg", "gg", "gg", "gt", "gg", "gg", "tg", "gg", 
                        "gg", "aa", "aa", "ac", "aa", "ca", "aa", "aa", "aa", "aa", "ac", 
                        "tt", "tt", "ct", "cc", "tt", "tt", "ct", "tc", "tc", "tt", "aa", 
                        "aa", "ag", "aa", "ga", "ga", "aa", "aa", "aa", "ag", "aa", "aa", 
                        "aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "tt", "tt", "tt", 
                        "tt", "tt", "gt", "tt", "tt", "tt", "tt"), dim = c(10L, 6L), dimnames = list(
                          NULL, c("genotype1", "genotype2", "genotype3", "genotype4", 
                                  "genotype5", "genotype6")))

genotype %>%
  as.data.frame() %>%
  mutate(across(everything(), ~str_split(.x, "", simplify = TRUE)))
#>    genotype1.1 genotype1.2 genotype2.1 genotype2.2 genotype3.1 genotype3.2
#> 1            g           g           a           a           t           t
#> 2            g           g           a           a           t           t
#> 3            g           g           a           c           c           t
#> 4            g           g           a           a           c           c
#> 5            g           t           c           a           t           t
#> 6            g           g           a           a           t           t
#> 7            g           g           a           a           c           t
#> 8            t           g           a           a           t           c
#> 9            g           g           a           a           t           c
#> 10           g           g           a           c           t           t
#>    genotype4.1 genotype4.2 genotype5.1 genotype5.2 genotype6.1 genotype6.2
#> 1            a           a           a           a           t           t
#> 2            a           a           a           a           t           t
#> 3            a           g           a           a           t           t
#> 4            a           a           a           a           t           t
#> 5            g           a           a           a           t           t
#> 6            g           a           a           a           g           t
#> 7            a           a           a           a           t           t
#> 8            a           a           a           a           t           t
#> 9            a           a           a           a           t           t
#> 10           a           g           a           a           t           t

创建于2023年3月19日,使用reprex v2.0.2

cfh9epnr

cfh9epnr2#

这也许是@jared_mamrot上面回答的一个不太简洁的版本...

as.data.frame(mGenotype) %>% 
  mutate(across(everything(),
                ~ substr(.,1,1),
                .names = "first_{.col}")) %>% 
  mutate(across(genotype1:genotype6,
                ~ substr(.,2,2),
                .names = "second_{.col}")) %>% 
  dplyr::select(!starts_with('genotype'))

但如果你像我一样,需要先一步一步地看事情,可能会有帮助。

lf5gs5x2

lf5gs5x23#

您可以按MARGIN=1unlist元素和t转换进行拆分。

apply(mGenotype, 1, strsplit, split="") |> sapply(unlist) |> t()
#       genotype11 genotype12 genotype21 genotype22 genotype31 genotype32 genotype41 genotype42 genotype51 genotype52 genotype61 genotype62
#  [1,] "g"        "g"        "a"        "a"        "t"        "t"        "a"        "a"        "a"        "a"        "t"        "t"       
#  [2,] "g"        "g"        "a"        "a"        "t"        "t"        "a"        "a"        "a"        "a"        "t"        "t"       
#  [3,] "g"        "g"        "a"        "c"        "c"        "t"        "a"        "g"        "a"        "a"        "t"        "t"       
#  [4,] "g"        "g"        "a"        "a"        "c"        "c"        "a"        "a"        "a"        "a"        "t"        "t"       
#  [5,] "g"        "t"        "c"        "a"        "t"        "t"        "g"        "a"        "a"        "a"        "t"        "t"       
#  [6,] "g"        "g"        "a"        "a"        "t"        "t"        "g"        "a"        "a"        "a"        "g"        "t"       
#  [7,] "g"        "g"        "a"        "a"        "c"        "t"        "a"        "a"        "a"        "a"        "t"        "t"       
#  [8,] "t"        "g"        "a"        "a"        "t"        "c"        "a"        "a"        "a"        "a"        "t"        "t"       
#  [9,] "g"        "g"        "a"        "a"        "t"        "c"        "a"        "a"        "a"        "a"        "t"        "t"       
# [10,] "g"        "g"        "a"        "c"        "t"        "t"        "a"        "g"        "a"        "a"        "t"        "t"

如果需要 Dataframe ,只需通过管道传输到另一个|> as.data.frame()

r8xiu3jd

r8xiu3jd4#

base R中使用read.fwf

read.fwf(textConnection(do.call(paste, c(as.data.frame(genotype), sep = ""))),
     widths = rep(1, max(nchar(c(genotype)) * ncol(genotype))))

相关问题