简体   繁体   中英

Error in write.table

 for(i in 1:4){
     write.table(x, file=cat("C:\\Users\\Desktop\\example_",i,sep=""),row.names=FALSE)}

Error in if (file == "") file <- stdout() else if (is.character(file)) { : argument is of length zero

How can I solve this error?? What are the differences between write.table , write.csv and write.csv2 ??

Using paste() or paste0() instead of cat() should resolve the issue you are having, as mentioned in the comment.

write.table() creates a dataframe

write.csv() uses "." for the decimal point and a comma for the separator.

write.csv2() uses a comma for the decimal point and a semicolon for the separator, the Excel convention for CSV files in some Western European locales.

read ?write.table for more details than what is in the answer I gave above.

I thought I might add a bit more to explain why cat() will not work.

if you look in the routine for cat() you will see that this is meant for sending output to stdout or some file connection.

function (..., file = "", sep = " ", fill = FALSE, labels = NULL, 
    append = FALSE) 
{
    if (is.character(file)) 
        if (file == "") 
            file <- stdout()
        else if (substring(file, 1L, 1L) == "|") {
            file <- pipe(substring(file, 2L), "w")
            on.exit(close(file))
        }
        else {
            file <- file(file, ifelse(append, "a", "w"))
            on.exit(close(file))
        }
    .Internal(cat(list(...), file, sep, fill, labels, append))
}

so, when cat is run it is sending its output to stdout in your case not back to the function calling it ( write.table() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM