Write Data Into Excel Using R – write.xlsx()

Filed Under: R Programming
Write Data Into Excel in R

Excel is one of the best and widely used tools for data storage and analysis. Even today, though we got many new tools for data analysis, the Excel love never ends. As an R user, you will get a couple of packages that help you to interact with R. Today, let’s see how we can write data into Excel using R.


Install the xlsx package

To write the data into Excel using R, you need to install the “xlsx” package. So, let’s install and load the package into the R environment.

#Install the required packages

install.packages('xlsx')

#Load the package 

library(xlsx)

Write Data into Excel Using R

Well, we have installed the required 'xlsx' package into R. Now, it’s time to write some data into Excel in R.

  • First, load the data into R.
  • Call the write.xlsx function.
  • Save the data into an file.

But, wait!

You might encounter a Java error. Worry not. It can be a result of an unknown Java version or no java installed on your PC.

It will also occur when the installed Java version is not pointed to the R installed folder.

If you have to install the Java latest 64-bit version, go here.

Fine, there will be no more stopping for you. In this process, make sure you set the working directory first and then proceed with writing data.

#Writing data into Excel

#Import data
df <- datasets::iris

#Define data and file name
write.xlsx(df, file = 'demodata.xlsx')
Write Xlsx 1

That’s great. We just wrote some data into a file. But, the fun never stops. You can do more with xlsx package. Let’s explore some of them.


Creating an Excel Workbook in R

You just wrote some data into Excel and I am sure you will be rolling! Well, let’s see how we can create an excel workbook and have some fun with it.

To create a workbook you have to make use of createWorkbook function.

# create new workbook

workb <- createWorkbook()

Now, let’s create a sheet in the created workbook and add some data to it.

# create a worksheet 

works <- createSheet(wb, sheetName = "new_data")

Add the Data Into WorkSheet

Now, we have created a new workbook and worksheet as well. So, we can add some data to the Excel sheet. Let’s see how we can do this.

#Add data into workbook

df <- datasets::iris
addDataFrame(df, sheet = works, row.names = FALSE, startRow = 1, startColumn = 1)

Well, we have added the data into the worksheet in the new workbook. You can see multiple arguments in the above code.

  • row.names – You can mention it as True or False.
  • start.row – You can mention the starting row of the data in the sheet.
  • start.column – You can mention the starting column in the sheet.

Save the Workbook

Finally, we have created a new workbook and sheet in it as well. We have added iris data into the excel sheet. So, as a final step, you have to save the workbook.

Upon saving, an excel workbook will get saved into your working directory as shown below with all the data we added.

#Save the workbook

saveWorkbook(workb, file = "new_workbook.xlsx")
New Workbook

Perfect!

We have successfully saved the workbook. You can see the new workbook, new worksheet, and the data we have added to it.

The xlsx package in R is really easy to use and offers many useful functions, where you can create excel files and add the data into them.

Feel free to visit the xlsx documentation and try out different functions with your data.


Ending Note

As Excel is one of the most popular data analysis and management tools, R offers a lot to deal with Excel. As we saw, using the xlsx package in R, we can create the excel sheets and write data into them.

Using this package you can save a lot of time going to excel and performing some tasks. Try creating your workbook and add some data into it using xlsx as shown in this article.

That’s all for now. Happy R!

More read: The Xlsx package in R.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content