4
library(gdata)
library(data.table)


# data.table fails here, data.frame doesn't -------------
df <- data.table(
  a = c('asdfasdf','asdf','asdgasdgasdgasdg','sdg'),
  b = runif(4,0,1)
  )
write.fwf(df,"df.txt")
write.fwf(data.frame(df),"df.txt")


# data.table fails here, data.frame doesn't -------------
df <- data.table(
  a = c('asd','qwe','ert','dfg'),
  b = runif(4,0,1)
)
write.fwf(df,"df.txt")
write.fwf(data.frame(df),"df.txt")


# data.table does not fail here, nor does data.frame ----
df <- data.table(
  a = c(1,5,4,7),
  b = runif(4,0,1)
)
write.fwf(df,"df.txt")
write.fwf(data.frame(df),"df.txt")

当我尝试写出 data.table 时的错误消息是我无法连接到write.fwf应该做的事情 - Error in setkey(ans, NULL) : x may no longer be the character name of the data.table. The possibility was undocumented and has been removed.

有人知道为什么吗?

4

1 回答 1

2

#5069当参数是 data.table时gdata:::write.fwf最终出现错误(有时)现在已在v1.8.11中修复。

总结这个问题,当[.data.table从包中的函数(此处write.fwf)调用时,它会尝试识别包是否支持 data.table,如果不是(在这种情况下,它不是),它会调用等效[.data.frame方法。在你的情况下,这会导致一个字符向量。下一行要求对setkey向量进行操作(将键设置为 NULL)。

一个简单的解决方法是检查输出[.data.frame是否仍然是 a data.table,如果是,则将键设置为 NULL。

再次感谢您的报告。

于 2013-11-11T19:04:30.730 回答