I’m trying to write a data frame to a gzip file but having problems.
Here’s my code example:
df1 <- data.frame(id = seq(1,10,1), var1 = runif(10), var2 = runif(10))
gz1 <- gzfile("df1.gz","w" )
writeLines(df1)
Error in
writeLines(df1): invalid ‘text’ argument
Any suggestions?
EDIT:
an example line of the character vector I’m trying to write is:
0 | var1:1.5 var2:.55 var7:1250
The class label / y-variable is separated from the x-vars by a ” | ” and variable names are separated from values by ” : ” and spaces between variables.
EDIT2:
I apologize for the wording / format of the question but here are the results:
Old method:
system.time(write(out1, file="out1.txt"))
# user system elapsed
# 9.772 17.205 86.860
New Method:
writeGzFile <- function(){
gz1 = gzfile("df1.gz","w");
write(out1, gz1);
close(gz1)
}
system.time( writeGzFile())
# user system elapsed
# 2.312 0.000 2.478
Thank you all very much for helping me figure this out.
writeLinesexpects a list of strings. The simplest way to write this to a gzip file would beThis will write it as a gzipped csv. Also see
write.tableandwrite.csv2for alternate ways of writing the file out.EDIT:Based on the updates to the post about desired format, I made the following helper (quickly thrown together, probably admits tons of simplification):
So the output looks like
And all that is necessary is to pass the gzfile in to writeLines to get the desired output.