How to Export a Data Frame to a CSV File in R (With Examples)

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(78, 85, 93, 90, 91),
                 assists=c(12, 20, 23, 8, 14))

#view data frame

  team points assists
1    A     78      12
2    B     85      20
3    C     93      23
4    D     90       8
5    E     91      14

There are three common ways to export this data frame to a CSV file in R:

1. Use write.csv from base R

If your data frame is reasonably small, you can just use the write.csv function from base R to export it to a CSV file.

When using this method, be sure to specify row.names=FALSE if you don’t want R to export the row names to the CSV file.

write.csv(df, "C:\\Users\\Bob\\Desktop\\data.csv", row.names=FALSE)

2. Use write_csv from reader package

An even faster way to export a data frame to a CSV file is with the write_csv function from the reader package. This is about 2x faster than write.csv and it never writes the row names from the data frame to a CSV file.


write_csv(df, "C:\\Users\\Bob\\Desktop\\data.csv")

3. Use fwrite from data.table package

Yet a faster way (and a recommended method for large datasets) to export a data frame to a CSV file is with the fwrite function from the data.table package. This function is about 2x faster than the write_csv method.


fwrite(df, "C:\\Users\\Bob\\Desktop\\data.csv")

Note that in each example we used double backslashes (\\) in the file path to avoid the following common error:

Error: '\U' used without hex digits in character string starting ""C:\U"

The Output

Each of the three methods above produce an identical CSV file. If we open this file with Excel, here’s what it looks like:

Export data frame to a CSV file in R

And if we open the CSV file with a text reader like Notepad, here’s what it looks like:

Export data frame to CSV in R

Related: How to Import CSV Files into R

One Reply to “How to Export a Data Frame to a CSV File in R (With Examples)”

  1. Hello Zach,

    I intialiazed an 3 dimensional array saved regression results to it in a loop(loop not shown):

    obj_1_strat_array <- array(0, dim = c(mm,5,ss),

    Then I saved it to a CSV:
    write.csv(obj_1_strat_array, "obj_1_strat_results.csv")

    However, when I import "obj_1_strat_results.csv" back into R, I do not get the same array that i orginally exported;

    obj_1_strat_array str(obj_1_strat_array)
    List of 225
    $ : chr [1:4] “White_non_Hispanic_or_Latino” “White_Hispanic_or_Latino” “Black_African_American” “Asian”
    $ : num [1:4] -0.0792 -0.1971 -0.5316 -47.1299
    [list output truncated]
    – attr(*, “dim”)= int [1:3] 5 5 9
    – attr(*, “dimnames”)=List of 3
    ..$ : chr [1:5] “White_non_Hispanic_or_Latino” “White_non_Hispanic_or_Latino” “White_Hispanic_or_Latino” “Black_African_American” …
    ..$ : chr [1:5] “snp_beta” “snp_se_beta” “snp_OR” “snp_p_value” …
    ..$ : chr [1:9] “snp_01” “snp_02” “snp_03” “snp_04” …

    Is there a straightforward way to get it back into it’s array format without rerunning the whole analysis? as.array() or something?

Leave a Reply

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