r - 在同一 session 中使用包 dplyr 和 data.table 会导致 mutate() 中的复制错误

标签 r data.table dplyr

我想使用data.table包的快速读取功能来加载一个巨大的csv文件。加载后我想将字符串变量转换为因子。但是当我想像这样对加载的文件进行变异时:

library(data.table)
library(dplyr)

df <- fread("df.csv")
df <- mutate(df, name = as.factor(name))

我收到此错误:

Error in mutate.data.table(df, df = df, df = ,  : 
  could not find function "copy

我尝试在进行变异之前分离 data.table 包,但这没有帮助。我仍然收到该错误。

有谁知道如何处理这个问题吗?非常感谢!

最佳答案

函数copy来自data.table包。因此,当您分离 data.table时,dplyr无法找到函数copy(由于某种原因,它是未正确导入)。

正如 Hadley 在评论中所说,这实际上是 dplyr 中的一个错误,在 dplyr 0.3 中不应再发生。

重现您的错误:

library(data.table)
library(dplyr)

# Creating the data.table
df <- data.table(name = 1:10)

#detaching data.table, this will cause the error
detach(package:data.table)

#dplyr can't find copy
mutate(df, name=as.factor(name))

Error in mutate.data.table(df, name = as.factor(name)) : 
  could not find function "copy"

请注意,加载两个包时不会发生错误:

library(data.table)
library(dplyr)
df <- data.table(name = 1:10)
mutate(df, name=as.factor(name))
    name
 1:    1
 2:    2
 3:    3
 4:    4
 5:    5
 6:    6
 7:    7
 8:    8
 9:    9
10:   10

关于r - 在同一 session 中使用包 dplyr 和 data.table 会导致 mutate() 中的复制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26145525/

相关文章:

r - 如何使用 [row,col] 距离索引对特定列进行行求和循环

r - mutate_column 使用 dplyr 中的计算列名

r - 如何在R中读取大型数据集的子集?

r - 刻度线和光栅边距之间的空间,ggplot2,R

r - 使用 mutate_at 中的粘贴添加到列

r - 在大 data.table 中操作字符串的最佳方法

r - 在 R 中用 data.table 对许多列求和,删除 NA

r - 在 geom_hline 上方突出显示条形图颜色

r - 使用数据表分组后包含列

r - 塑造数据并影响 R 中的计数列