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.
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.
install.packages('writexl')
Openxlsx: The R library which is very helpful in manipulating the data and export to Excel. To install this package, run the below code.
1
install.packages('openxlsx')
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.
#creates a dataframe in R
mydataframe<- data.frame(name=c('John','Angelina','Lisa','Joseph','Antonio'),
age=c(32,30,26,23,28),
gender=c('male','female','female','male','male'))
After creating this data frame, let’s export it to Excel using the writexl function.
#import the required library
library(writexl)
#exports the data to excel and dumps it to the working directory/folder
#to see your working directory run 'getwd()' in R studio
mydataframe<- data.frame(name=c('John','Angelina','Lisa','Joseph','Antonio'),
age=c(32,30,26,23,28),
gender=c('male','female','female','male','male'))
write_xlsx(mydataframe, 'export_a_dataframe_to_excel.xlsx')
The output excel file is shown below.
#r programming #excel #data