How to Export a data frame to Excel in R With Examples

What if I tell, that you can export data frames to excel in R within a couple of minutes? This is possible with the help of functions like writexl and openxlsx. Seems interesting? In this tutorial, we are going to see how we can export data frames to excel in R.

Before we dive into the tutorials, let’s understand what is writexl and openxlsx functions.

Start with installing required packages

writexl: writexl is a library in R, which is capable of writing R data frames into excel files. To install this package, run the below code.

Openxlsx: The R library which is very helpful in manipulating the data and export to Excel. To install this package, run the below code.


Create a data frame and Export to Excel

Well, I hope you have successfully installed those packages. Now let’s create a data frame in R and export it to Excel using ‘writexl’ function.

After creating this data frame, let’s export it to Excel using the writexl function.

The output excel file is shown below.

exporting-a-datafame-in-r-to-excel

Import a dataset in R and export it to Excel

You have exported a simple data frame in R to excel in the above section. Here we are going to import a data set or a CSV file and export it to Excel file.

data-frame-to-excel

Export a data frame to Excel by manipulating data using openxlsx

Till now, we have exported the data frames to Excel without any data manipulations. But what if you need to export the data with some meaningful changes? There comes the use of functions ‘dplyr’ and ‘openxlsx’.

Dplyr library will assist you in data manipulation i.e. you can make changes to the dataset by adding a column, a row, or a percentage and more.

Openxlsx function allows you to make all possible changes such as adding a style, font, color and you can also create a worksheet using this function.

The below image shows the basic data set available in R.

openxlsx-function-in-r

Let’s make some changes in the header of the data frame using dplyr. Here we are changing the color of headlines and alignment to center. We have also changed the values in the Illiteracy column as percentages.

Execute the below code to create excel file with new header style.

The data frame with a new headline style and alignment style is shown below.

new-header-style

Create a new table style in R and export to Excel

You can also change the table theme or style by adding this piece of code to the above code.

new-header-style

Alright, we have created data frames, imported data frames, and exported to excel using the writexl and openxlsx functions. We did data manipulation and exported the data frame with a new look to excel.

But, there are some other possibilities that you can do in R. Let’s look into that.


Can we create a workbook in R and Export to Excel?

The answer will be a big ‘YES’. You can create an entire workbook and add worksheets into that with some data along with some changes which will be meaningful and make sense to readers.

Let’s create!!!

In the below workbook, you can see the income column has been added with a dollar sign and, values are separated by commas. These numerical format changes will make sense and convey the information in a more detailed way.

new-header-style

Conclusion

R is a perfect language for data analysis. You can do wonders using data in R. In this tutorial we have gone through the various ways of exporting a data frame to excel. You got to know how you can manipulate the data and bring some life into that before exporting to excel.

That’s all for now, stay connected for more R tutorials and I am sure that we can continue playing with some more data the next time. Happy exporting!!!

By admin

Leave a Reply

%d bloggers like this: