Write CSV in R

How to Write CSV in R

I often write to CSV with R in order to save data and share files with others. One way to output a csv from R is with the command write.csv. Here is an example of how to write CSV in R:

# Write CSV in R
write.csv(MyData, file = "MyData.csv")

The above writes the data data frame MyData into a CSV that it creates called MyData.csv. Note that the file is written to your working directory.

Most of the time I don’t want to include row names in the CSV. To omit the row names, I add a comma and then row.names=FALSE. row.names=F will also work if you’re into the whole brevity thing.

# Write CSV in R
write.csv(MyData, file = "MyData.csv",row.names=FALSE)

Sometimes I also want to leave out the NAs. To omit NAs, add a comma and then na=””.

# Write CSV in R
write.csv(MyData, file = "MyData.csv",row.names=FALSE, na="")

If you want to leave out the column names it is a little more complicated. write.csv is what is called a “convenience wrapper” and so has few features. Instead, use write.table and specify  that sep=”,” and col.names=FALSE.

# Write CSV in R
write.table(MyData, file = "MyData.csv",row.names=FALSE, na="",col.names=FALSE, sep=",")

There are other options that can be used with write.csv. Since write.csv is related to write.table, see official R-manual page on write.table to learn more: http://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

15 thoughts on “Write CSV in R

  1. Let Aurn

    Hi.

    I want to omit NAs, but in my out file, I have something like this :
    1, 2, 3.4, 5.5, , , , , , ,
    Can I remove commas too ?
    Thanks

    Reply
      1. Justin@RProgramming.net Post author

        I don’t think you can specify to leave out commas within the data using write.csv. You may want to try sub() from the grep package. Here’s a simple example where I use sub() to remove any string of seven commas and six spaces. Obviously this will only work for that exact string but it will get you started. Good luck!

        Note that the quotation marks below won’t work if you copy paste into R due to WordPress trying to be helpful. Be sure to fix them.
        data <- "1, 2, 3.4, 5.5, , , , , , ," cleandata <- sub(", , , , , , ,","",data)

        Reply
  2. Pingback: Write CSV in R with Examples using write.csv | ...

    1. Justin@RProgramming.net Post author

      You’re right, thanks for correcting my error! In fact, even col.names won’t work with write.csv. You have to use write.table, as shown in the updated code.

      Reply
  3. Conal Monaghan

    Hi all,
    Several collegues and I have tried running this code and always fall into the same error “Error in get(x, envir = envir) : object ‘x’ not found”. This error returns regardless of the matrix used. Has anyone run into similar issue? I have included an example below.

    > library(WriteXLS)
    > testPerl(perl = “perl”, verbose = TRUE)
    Perl found.
    All required Perl modules were found.
    > Excelput= cbind(4, 3, 2, 4)
    [,1] [,2] [,3] [,4]
    [1,] 4 3 2 4
    > WriteXLS(as.data.frame(Excelput), ExcelFileName = “MODELS.xls”, row.names = TRUE, col.names = TRUE)

    Error in get(x, envir = envir) : object ‘4’ not found

    Regards,
    Conal

    Reply
    1. Justin@RProgramming.net Post author

      Sorry, I don’t know what the problem is but I have had good luck with the xlsx package for writing excel files with R.

      Reply
    1. Justin@RProgramming.net Post author

      read.csv() will overwrite existing files. If you have a file called “examplefile.csv” that you want to replace, use write.csv(MyData, file = “examplefile.csv”) where MyData is whatever you want to be in the file and examplefile.csv will be replaced. The old examplefile.csv will be gone and the newly created examplefile.csv will be in it’s place.

      If you want to update an existing file (for example, by adding another record) you would read in the existing csv to R using read.csv, make whatever changes are needed to the data, and then replace the existing csv using write.csv.

      Reply
    1. Justin@RProgramming.net Post author

      If your data is in CSV format you can convert it to CSV and use the method above. If your data is in xls or xlsx format you can use a package like “xlsx” to read the data in a similar way. If your data are on paper or otherwise not yet stored in a data file I recommend entering them into a CSV.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *