Member-only story

How To Export .XLSX/ .CSV Files In R!

Manpreet Singh
2 min readFeb 18, 2021

--

Exporting data with R is one of the most important things you can possibly do with this language, let’s go ahead and show you how to do this!

Exporting .XLSX Files

In order to export a .XLSX file from R we need to use the xlsx package, we do by importing this package like this:

library(xlsx)

Next up we want to make sure you already have a dataset loaded in, let’s say we have our dataset stored in a variable called “df” like below

df <- c(1,2,3,4,5,6)

Next up, if we would want to export this data frame into a .XLSX file we would want to find out where we want to store this dataset at, let’s say we want to store it on our desktop, here is the code to do so:

write.xlsx(df, file = “sampleworkbook.xlsx”, sheetName = “sheet1”, append = FALSE)

Awesome! That’s pretty much it! Now let’s do a .CSV File!

Exporting .CSV Files

Csv files are very easy to export in R, in order to do this we want to bring in a data frame, in this case we’ll use the same data frame as before:

df <- c(1,2,3,4,5,6)

Next up all we want to do is figure out where we want to export this file at, we want to grab that directory and we want to use the “write.csv” command within R, we essentially use that command, use our df as imported before, and export it using the “file” command, this is how we would do it:

write.csv(df, file = “/Users/users/Desktop/df.csv”)

There you go, you should see a .csv file imported on your desktop now!

--

--

Manpreet Singh
Manpreet Singh

No responses yet