How to export data frame in R to excel?

R ProgrammingServer Side ProgrammingProgramming

To export data frame in R to excel can be done with the help of write.xlsx function of xlsx package. We would need to pass the data frame name, the file name and the sheet name in which we want to save the data frame. For example, if we have a data frame called df with file name Excel_df and on sheet1 then the data frame will be saved by using the below command −

write.xlsx(df, file="Excel_df.xlsx", sheetName = "Sheet1")

Consider the below data frame −

Example

 Live Demo

x<-rnorm(20)
y<-rnorm(20)
z<-rnorm(20)
df<-data.frame(x,y,z)
df

Output

        x         y           z
1   0.04319856   1.6785739    1.77106752
2  -0.36870535  -0.5876720   -0.13563273
3  -1.11985900  -1.1756613    0.99374670
4   0.07497696  -0.1004814    0.59998832
5   0.47198699  -0.9880506    0.56023277
6  -0.40857098   0.4455695   -1.01059422
7   0.19381445  -1.0285333    1.35449579
8  -1.56931369   0.3467661    0.09928035
9   0.01467629  -0.9501424   -0.27383159
10 -0.70354093  -1.2913159   -0.26957811
11  0.13393603   0.2261357   -0.26161987
12  1.26584454   1.4669579    0.23035925
13  1.24692292   0.8695646   -0.94605061
14  0.73639868   1.3668048   -0.91025497
15 -1.59570185  -1.8947813    2.01405239
16  1.97949873   0.8275024    0.10388833
17  0.28805664   1.4392612    1.91595195
18  0.26419780   1.0322670    0.04117154
19 -0.19036166  -0.1917548   -1.41957429
20  0.20173227   1.4283021    0.25179883

Loading xlsx package and exporting the data frame df in Excel −

Example

library("xlsx")
write.xlsx(df, file="ExcelExport.xlsx", sheetName = "Sheet1")

Output

raja
Updated on 10-Feb-2021 05:04:53

Advertisements