bind_rows 在 dplyr 中抛出异常错误

bind_rows in dplyr throwing unusual error

希望我没有重复一些以前存在的问题。我在 32 位 Win7 机器上工作,R V=3.2.0,dplyr V=0.4.1,RStudio 0.98.1103。

有问题的文件是两个 CSV 文件,读入 vars (x,y / sep = "|", header = TRUE, stringsasFactors = FALSE),它们来自同一个 Oracle table。用于生成这两个文件的查询提取了完全相同的变量(共 29 个)。

identical(names(x), names(y) > TRUE

但是,当我加载 dplyr 包并尝试将“bind_rows”用作 dat <- bind_rows(x, y) 时,出现以下错误:

> bind_rows(x,y)
Error: incompatible type (data index: 2, column: 'rmnumber', was collecting: integer (dplyr::Collecter_Impl<13>), incompatible with data of type: factor
In addition: Warning messages:
1: In rbind_all(list(x, ...)) :
  Unequal factor levels: coercing to character
2: In rbind_all(list(x, ...)) :
  Unequal factor levels: coercing to character
3: In rbind_all(list(x, ...)) :
  Unequal factor levels: coercing to character

我查看了 'rmnumber' 列并验证了该列中的所有内容都是预期的数字或 "NA",对于 table 中的 NULL 值也是预期的。我也试过 bind_rows(list(x,y)) 它返回了同样的错误。

原始 "rbind" 在这些变量上工作得很好,没有明显的精度损失。

有人见过这个错误吗?除了使用 rbind 之外,您有任何潜在的解决方案吗?

谢谢!

#

我认为这没有用,但我构建了自己的 dfs,当然 'bind_rows' 工作得非常完美:

> x.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))
> y.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))
> bind_rows(x.df, y.df)
Source: local data frame [30 x 4]

   first_name last_name rmnum         addy
1         abc       def     1 some_address
2         abc       def     2 some_address
3         abc       def     3 some_address
4         abc       def     4 some_address
5         abc       def     5 some_address
6         abc       def     6 some_address
7         abc       def     7 some_address
8         abc       def     8 some_address
9         abc       def     9 some_address
10        abc       def    10 some_address
..        ...       ...   ...          ...

正在验证 class 列

> identical(sapply(x, class), sapply(y, class))
[1] FALSE

> class(x$rmnumber);class(y$rmnumber)
[1] "integer"
[1] "character"

我想不通的是为什么它们不同。信息来自完全相同的table,并且使用完全相同的代码将它们读入变量。

锁定解决方案

非常感谢@Pascal 帮我解决了这个问题。一个简单的数据类型转换解决了我的问题:

    y$rmnumber <- as.integer(y$rmnumber)
> dat2 <- bind_rows(x,y)
> dat2
Source: local data frame [99,884 x 24]

错误消息说:"in one data.frame, 'rmnumber' in of class integer and in the other data.frame, 'rmnumber' is of class factor. I cannot bind different classes together"。

让我们用你的例子

x.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))
y.df <- data.frame(first_name = c("abc"), last_name = c("def"), rmnum = (1:15), addy = ("some_address"))

我们检查 "x.df" 和 "y.df" 的每一列的 class:

sapply(x.df, class)
# first_name  last_name      rmnum       addy 
#  "factor"   "factor"  "integer"   "factor" 


sapply(y.df, class)
# first_name  last_name      rmnum       addy 
#  "factor"   "factor"  "integer"   "factor" 

一切正常,class之间data.frames是一致的。现在,让我们把 "y.df$rmnum" 变成因子:

y.df$rmnum <- factor(y.df$rmnum)
class(y.df$rmnum)
# [1] "factor"

现在尝试绑定:

bind_rows(x.df, y.df)

Error: incompatible type (data index: 2, column: 'rmnum', was collecting: integer (dplyr::Collecter_Impl<13>), incompatible with data of type: factor

同样的错误信息。因此,在您的一个 data.frame 中,'rmnumber' 是整数,而在另一个中,'rmnumber' 是一个因子。您必须将因式分解 'rmnumber' 变成整数,或者相反。