2

假设我有一个要合并到单个 data.table 中的文件列表。我解决这个问题的基本方法是做这样的事情:

files <- dir(...) # The list of files to be combined

read.data <- function(loadfile) {
    data.dt <- data.table(read.csv(loadfile));
}

data.dt <- data.table(file = files)[, read.data(file), by = file]

这种方法的问题是当你得到空的 data.tables 时(由只包含标题行的空文件引起)。

Error in `[.data.table`(data.table(file = files), , read.data(file),  :
columns of j don't evaluate to consistent types for each group

有没有办法让 data.table 无缝地正确连接空白或 NULL 值?这样你就可以做类似的事情

if(dim(data.dt)[1] == 0) {
    data.dt <- NULL
}

这应该可以解决我遇到的大部分问题。

编辑:我应该指出我已经使用 plyr 例程实现了这个逻辑。ldply() 完美地工作,但不幸的是,一旦您尝试传递多个文件,它就会非常缓慢且占用大量内存。

4

1 回答 1

2

这是data.table. 我在这里长大,所以它不会被遗忘。

一个更简单的例子是:

DT = data.table(a=1:3,b=1:9)
DT
      a b
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 1 4
[5,] 2 5
[6,] 3 6
[7,] 1 7
[8,] 2 8
[9,] 3 9
DT[,if (a==2) NULL else sum(b),by=a]
Error in `[.data.table`(DT, , if (a == 2) NULL else sum(b), by = a) : 
  columns of j don't evaluate to consistent types for each group

以下错误是正确的:

DT[,if (a==2) 42 else sum(b),by=a]
Error in `[.data.table`(DT, , if (a == 2) 42 else sum(b), by = a) : 
  columns of j don't evaluate to consistent types for each group

并使用以下方法进行更正:

DT[,if (a==2) 42L else sum(b),by=a]
     a V1
[1,] 1 12
[2,] 2 42
[3,] 3 18

NULL但在修复错误之前,我想不出解决方法。

于 2011-09-08T21:49:36.733 回答