[R] export only if data is available

Ben Tupper btupper at bigelow.org
Fri Feb 8 15:00:46 CET 2013


Hi,

On Feb 8, 2013, at 2:49 AM, Mat wrote:

> Hello together,
> 
> i have a problem with an export.
> I want to export a data.frame, only if there ist data in it.
> My data.frame (CO.PHB) ist empty but with this code, the .txt-file gets
> exported every time, with no content.
> 
> {
> pfad<-paste(paste(paste("C:/Test/","Reminder",sep=""),
> "Orders",sep="/"),"txt",sep=".")
> write.xls( Orders,pfad,colNames = TRUE,sheet = 1,from = 1,rowNames = FALSE )
> if(dim(CO.PHB)[1]>0){
> write.table( CO.PHB,pfad,row.names=FALSE,sep=" | ")
> }}
> 
> how should my code look like, if i want only an Export, when there is data
> in the data.frame?
> 

Try using nrow() to test the data frame first.

if (nrow(CO.PHB) != 0) {
   # write the file
} else {
   cat("The data frame is empty\n")
}

Cheers,
Ben



> Thanks Mat
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/export-only-if-data-is-available-tp4657922.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list