quant quant - 17 days ago 8
R Question

How to write CSV with per-variable counts of a data.table?

Assuming I have a data table,

td
, with dimensions 1000x4, with column names
x1,x2,x3,x4
. If I do
td[,.N,x1]
I will get the counts for each value of the
x1
variable.

What I would like to do, is to write all these values in a csv in one go,below each other, so I try

x <- c("x1","x2","x3","x4")
l_ply(x, function(x) {
write.csv(td[,.N,by=x],file="test.csv")
}


but I get an error:


The items in the 'by' or 'keyby' list are length .... Each must be
same length as rows in x or number of rows returned by i ...


Any ideas ?

Answer

You have to use write.table(..., append=TRUE) because write.csv() wrapper doesn't pass it through(!)

Then (and if you don't want a per-variable header before each variable's section, use col.names=F ; see Write column header once only, when writing data with write.table(append=T) ):

require(data.table)
require(plyr)
x <- c("x1","x2","x3","x4")
td <- data.table(x1=sample.int(2,5,replace=T), x2=sample.int(2,5,replace=T), x3=sample.int(2,5,replace=T), x4=sample.int(2,5,replace=T))

l_ply(x, function(x) {
  write.table(td[,.N,by=x], file="test.csv", append=T, quote=F, row.names=F)
})
Comments