bind_rows_(x,.id)中的错误:无法将列从系数转换为数值 [英] Error in bind_rows_(x, .id) : Column can't be converted from factor to numeric

查看:87
本文介绍了bind_rows_(x,.id)中的错误:无法将列从系数转换为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有十个数据集,这些数据集已使用 xlsx 库从Excel文件中读取并存储在小节中.我要合并它们.

I have ten datasets that have been read from Excel files, using the xlsx library, and stored in tibbles. I want to merge them.

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

Here are example datasets. The number of variables differ between datasets, and some variables are only in one dataset. The value of the person variable will never overlap.

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个变量.我尝试过

The actual datasets usually have ~50 rows and ~200 variables in them. I have tried

    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文件.

I have searched Stackoverflow, and I found questions regarding this, and most answers center on trying to convert the variables to another class. But I don't care which classes my variables have, because I will just write the merged dataset to a CSV-file or Excel file.

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

Isn't there some kind of simple workaround?

推荐答案

我认为这应该有效:

library(plyr)
all_data <- rbind.fill(data1,data2)

这篇关于bind_rows_(x,.id)中的错误:无法将列从系数转换为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆