13

我有十个使用xlsx库从 Excel 文件中读取并存储在小标题中的数据集。我想合并它们。

以下是示例数据集。数据集之间的变量数量不同,并且某些变量仅在一个数据集中。person变量的值永远不会重叠。

data1 <- tibble(person = c("A","B","C"),
    test1 = as.factor(c(1,4,5)), 
    test2 = c(14,25,10),
    test3 = c(12.5,16.0,4),
    test4 = c(16,23,21),
    test5 = as.factor(c(49,36,52)))

data2 <- tibble(person = c("D","E","F"),
    test1 = c(8,7,2), 
    test3 = c(6.5,12.0,19.5),
    test4 = as.factor(c(15,21,29)),
    test5 = as.factor(c(54,51,36)),
    test6 = c(32,32,29),
    test7 = c(13,11,10))

实际的数据集通常有大约 50 行和大约 200 个变量。我努力了

    all_data <- dplyr::bind_rows(data1,data2)

希望能得到这样的结果

# A tibble: 6 x 8
  person test1 test2 test3 test4 test5 test6 test7
   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1      A     1    14  12.5    16    49    NA    NA
2      B     4    25  16.0    23    36    NA    NA
3      C     5    10   4.0    21    52    NA    NA
4      D     8    NA   6.5    15    54    32    13
5      E     7    NA  12.0    21    51    32    11
6      F     2    NA  19.5    29    36    29    10

但是我得到了这个错误

Error in bind_rows_(x, .id) : Column `test1` can't be converted from factor to numeric

我搜索了 Stackoverflow,发现了与此相关的问题,大多数答案都集中在尝试将变量转换为另一个类。但我不在乎我的变量有哪些类,因为我只会将合并的数据集写入 CSV 文件或 Excel 文件。

没有某种简单的解决方法吗?

4

3 回答 3

12

我认为这应该有效:

library(plyr)
all_data <- rbind.fill(data1,data2)
于 2017-10-17T11:45:00.020 回答
9

由于文件通常很小(几百行),并且您只是想将两个文件合并并写入一个新文件,我认为我们可以将所有列转换为字符,因此 和 中的公共列data1data2具有相同的类型。

library(dplyr)
bind_rows(mutate_all(data1, as.character), mutate_all(data2, as.character))
于 2017-10-17T11:45:56.640 回答
0

data1 中的 test1 属于类因子,而 data2 中的 test1 属于数字类。组合因子类和数值类会导致此问题。解决方案要么将 data1 和 data2 中的 test1 转换为因子,然后使用all_data <- dplyr::bind_rows(data1,data2)

或者

data.table::rbindlist(data1,data2)

于 2020-05-06T19:30:22.647 回答