【问题标题】:separate column with unknown name名称未知的单独列
【发布时间】:2018-11-23 12:43:53
【问题描述】:

我有一个这样的数据框:

structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2", 
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7", 
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6", 
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

我想要做的是将第二列分成单独的列,用“。”分隔。在列名中。但是,并不总是知道列的名称是什么,这就是为什么我不能在 dplyr 的“单独”函数中使用列的名称。

我尝试了以下方法:

library(dplyr)
library(stringr)
library(tidyr)

# get new column names
ids <- unlist(strsplit(names(df)[-1],
                              split = ".",
                              fixed = TRUE))

# get name of column to split
split_column <- names(df)[-1]

df %>%
separate(split_column, into = ids, extra = "merge")

这在我正在使用的脚本文件中有效,但是当我获取脚本时,我收到以下错误:

Error: `var` must evaluate to a single number or a column name, not a character vector

为什么当我在 RStudio 中正常运行它时它会起作用,但是当我获取脚本时它会抛出这个错误? 另外,这是将未知名称的列实际拆分为名称未知的新列的最佳方式吗?

我在另一个脚本文件中使用以下代码获取脚本:

system(paste("Rscript script.R", opt$m, opt$o))

其中 opt$m 和 opt$o 是目录路径。这适用于我拥有的类似脚本,但使用上面的脚本会引发错误。

我希望有某种功能,例如单独的_at,但目前还不存在..

【问题讨论】:

  • 对我来说很好用:R 版本 3.4.2,tidyr_0.8.1 dplyr_0.7.7
  • 在使用 Rstudio 与使用源脚本时,您可能有不同版本的 R 或包。
  • @zx8754 是的,我认为一定是这样 - 我会检查版本
  • 无法复制。两件事:您可能希望在分离之前通过 trimws() 运行您的拆分列,否则您的第一列将是您提供的数据的空白。其次 - 考虑调用tidyr::separate(),因为您的脚本可能出于某种原因访问不同的包。
  • (另外,单独来自tidyr,而不是dplyr

标签: r dplyr tidyr


【解决方案1】:

你可以使用strsplit()

split <- do.call(rbind, strsplit(gsub("\\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1[] <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))

> df1
   header   ST adk fumC gyrB icd mdh purA recA
1       1   10  10   11    4   8   8    8    2
2       2   48   6   11    4   8   8    8    2
3       3   58   6    4    4  16  24    8   14
4       4   88   6    4   12   1  20   12    7
5       5  117  20   45   41  43   5   32    2
6       6 7036 526    7    1   1   8   71    6
7       7  101  43   41   15  18  11    7    6
8       8 3595 112   11    5  12   8   88   86
9       9  117  20   45   41  43   5   32    2
10     10  744  10   11  135   8   8    8    2

数据

df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2", 
                                                                     " 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7", 
                                                                     " 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6", 
                                                                     " 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

【讨论】:

    【解决方案2】:

    与您的示例几乎相同的解决方案,只是做了一些调整。假设您要删除列中的'*',我就是这样做的:

    library(tidyverse)
    library(hablar)
    
    # Vector of new column names
    ids <- simplify(strsplit(names(df)[-1], 
                             split = ".", 
                             fixed = T))
    
    # Seperate second column
    df %>%
      mutate_at(2, funs(trimws(gsub("\\*", "", .)))) %>%
      separate(2, into = ids, extra = "merge", sep = " ") %>% 
      retype()
    

    给你:

    # A tibble: 10 x 9
       header    ST   adk  fumC  gyrB   icd   mdh  purA  recA
        <int> <int> <int> <int> <int> <int> <int> <int> <int>
     1      1    10    10    11     4     8     8     8     2
     2      2    48     6    11     4     8     8     8     2
     3      3    58     6     4     4    16    24     8    14
     4      4    88     6     4    12     1    20    12     7
     5      5   117    20    45    41    43     5    32     2
     6      6  7036   526     7     1     1     8    71     6
     7      7   101    43    41    15    18    11     7     6
     8      8  3595   112    11     5    12     8    88    86
     9      9   117    20    45    41    43     5    32     2
    10     10   744    10    11   135     8     8     8     2
    

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 2015-11-12
      • 2019-05-07
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多