rsplitdata.tablewrite.table

Randomly split a data table and make output files in R


I want to randomly split a data table into n number of outputs; then I want to write.table those outputs for each list. So, in test I want to write a file for each list within test.

library(data.table)

set.seed(100)

dt <- data.table(x=rnorm(1000))

n <- 10 # number of data sets

# randomly splits dt into n number of outputs
test <- split(dt, sample(1:n, nrow(dt), replace=T))

# writing tables for each sublist within test
# write.table(test)
# names <- paste0("output", n, ".txt", sep="")

Solution

  • We can use fwrite as it is a data.table and is much faster

    library(data.table)
    lapply(names(test), function(nm) fwrite(test[[nm]], paste0("output", nm, ".txt")))
    

    The header 'x' is the column name and if we need some custom formatting, it can be done with cat

    lapply(names(test), function(nm) 
          cat(test[[nm]][[1]], file = paste0("output", nm, ".txt"), sep = "\n"))
    

    Or as @chinsoon12 mentioned in the comments, specify col.names = FALSE (by default it is TRUE in fwrite)

    lapply(names(test), function(nm) fwrite(test[[nm]],
              paste0("output", nm, ".txt"), col.names = FALSE))