0

I want to make a function to save dataframe, so I coded as below:

 save<-function(x)
{
write.table(x, file=paste(x,"csv", sep="."))
write.table(x, file=paste(x,"txt", sep="."))
}

but I got errors:

 save(summary1)

Error in file(file, ifelse(append, "a", "w")) : invalid 'description' argument In addition: Warning message: In if (file == "") file <- stdout() else if (is.character(file)) { : the condition has length > 1 and only the first element will be used

what's wrong?

1 Answer 1

1

Your filename is not a character string, it looks like your filename is an attempt to paste an entire dataframe as a string along with 'csv' (the paste(x, 'csv', ...). This is because x is a dataframe, not a string, so R is complaining because it is not sure how to convert an entire dataframe to a single string.

If you want to save the file as 'x.csv' just do file="x.csv".

If you want the user to be able to specify the filename you could do:

 save<-function(x, fname)
{
write.table(x, file=paste(fname,"csv", sep="."))
write.table(x, file=paste(fname,"txt", sep="."))
}
save(summary1, "summary") # saved as summary.csv
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.