Domanda

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")

The error message when i try to write out the data.table is something I'm unable to connect to what write.fwf should be doing - 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.

Does anybody know why?

È stato utile?

Soluzione

#5069 that gdata:::write.fwf ended up with error (at times) when the argument is a data.table is now fixed in v1.8.11.

To summarise the issue, when [.data.table is called from within a function (here write.fwf) in a package, it tries to identify if the package is data.table aware, and if not (in this case, it's not), it calls the equivalent [.data.frame method. And in your case, that lead to a character vector. And the next line called for a setkey operation on a vector (to set the key to NULL).

A simple fix was to check if the output from [.data.frame is still a data.table and if so set the key to NULL.

Thanks again for reporting.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top