简体   繁体   中英

Using ifelse with write.table

I'm writing a file using ifelse:

ifelse("out.csv" %in% list.files(),
       write.table(summary, "out.csv", sep=",", col.names=FALSE, row.names=FALSE, append=TRUE),
       write.table(summary, "out.csv", sep=",", row.names=FALSE)
)

It seems to work fine but throws up an error: replacement has length zero

Any risk to using this code or a better way to do things?

Thanks

As noted in the help, ifelse evaluates both the true and false arguments regardless of the value of the condition. What you want is a plain if .

if("out.csv" %in% list.files()) {
   write.table(summary, "out.csv", sep=",", row.names=FALSE,
       col.names=FALSE, append=TRUE)
} else {
   write.table(summary, "out.csv", sep=",", row.names=FALSE)
}

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