How to read and write Excel files in R or import data from and export
data to Excel files in R, using read_excel()
(readxl), and
write_xlsx()
(writexl).
Below are the functions and the package you need to install.
Activity | Package | Function |
Read XLSX | readxl | read_excel() |
Read XLS | readxl | read_excel() |
Write XLSX | writexl | write_xlsx() |
The .xlsx or .xls file formats are the standard spreadsheet files for Microsoft Excel in Windows and macOS.
To read or import Excel (XLSX or .xlsx) files in R, first install the "readxl" package:
Then load the package with the following:
After the installation and loading of the "readxl" package, you can read the XLSX or .xlsx file.
If you set or have the working directory as the folder containing the file, use the line of code below:
Or specify the full path of where the file is located:
Note that the "sheet" argument is set to 1, which is also the default. This can be changed to other sheet numbers such as 2, 3 and so on or even sheet names, such as "Results" with the quotation marks.
To remove the initial messages, you can turn the XLSX input to a dataframe as below:
To read or import XLS or .xls files in R you can use the following line of code:
Or specify the full path:
The dataframe named dtfrm will be used:
dtfrm = data.frame(Group = c("A", "B", "B", "C", "D"),
ID = c("A02", "B12", "B15", "C04", "D07"),
Score = c(9, 8, 8, 10, 7),
Position = c(2, 3, 3, 1, 5))
dtfrm
Group ID Score Position
1 A A02 9 2
2 B B12 8 3
3 B B15 8 3
4 C C04 10 1
5 D D07 7 5
To write or export data to Excel or .xlsx files in R, first install the "writexl" package:
Then load the package with the following:
After the installation and loading of the "writexl" package, you can save the XLSX or .xlsx file to the working directory with the following line of code:
Or specify the full file path of where you want to save it:
The output of the single sheet should look like this in Excel:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes