quant
quant

Reputation: 4492

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 ?

Upvotes: 1

Views: 291

Answers (3)

smci
smci

Reputation: 33970

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

Upvotes: 1

Martin
Martin

Reputation: 1114

You can also use rbindlist from data.table:

write.csv(
    file = "./tmp.csv",
    x = rbindlist(lapply(x,function(y){
        dt[,.N,by = y]
    }))
)

Upvotes: 0

akrun
akrun

Reputation: 887951

We could use a for loop

for(val in x) {
     write.table(td[, .N, by = c(val)], file = "test.csv", append=TRUE)
 }

Upvotes: 0

Related Questions