Data Science Fundamentals (R): Import & Export Data from Excel — xlsx | by Carrie Lo | Towards Data Science

Data science is a very broad topic, before entering into this enormous forest, you ought to know the most fundamental part which is importing and exporting data correctly. Without the data, you cannot perform all the machine learning techniques or draw insightful analysis. You may think that importing and exporting are very easy, just one simple code with the file name inside. I had the same feeling before, but later on, I realized that there are some key parameters that need to be changed (especially if the data involves text), otherwise, you will not get the data as the way that you want. You will see the examples below.
This article would give you a summary of using an R package, xlsx. You will learn how to import and export the excel files with different functions and what the differences are between them.
Other import and export packages are discussed in series.
medium.com
xlsx
Read, write, format Excel 2007 and Excel 97/2000/XP/2003 files
The xlsx package gives programmatic control of Excel files using R. A high-level API allows the user to read a sheet of an xlsx document into a data.frame and write a data.frame to a file. Lower level functionality permits the direct manipulation of sheets, rows, and cells. For example, the user has control to set colors, fonts, data formats, add borders, hide/unhide sheets, add/remove rows, add/remove sheets, etc.
The input data involves English text, number, Traditional Chinese text, and Simplified Chinese text.
At the end of this demonstration, you will know what options should be specified to import and export data with different formats of context in R.
Function to test (default settings)
read.xlsx (file, sheetIndex, sheetName=NULL, rowIndex=NULL, startRow=NULL, endRow=NULL, colIndex=NULL, as.data.frame=TRUE, header=TRUE, colClasses=NA, keepFormulas=FALSE, encoding=”unknown”, password=NULL, ...)
read.xlsx2 (file, sheetIndex, sheetName=NULL, startRow=1, colIndex=NULL, endRow=NULL, as.data.frame=TRUE, header=TRUE, colClasses=”character”, password=NULL, ...)
write.xlsx (x, file, sheetName=”Sheet1", col.names=TRUE, row.names=TRUE, append=FALSE, showNA=TRUE, password=NULL)
write.xlsx2 (x, file, sheetName=”Sheet1",col.names=TRUE, row.names=TRUE, append=FALSE, password=NULL, ...)
#############library(xlsx)############## read excel xlsx - method 1xlsx_df = read.xlsx(file="Reference_Sample.xlsx", sheetName="Sample_Sheet", header=T, stringsAsFactors=F, encoding="UTF-8")
str(xlsx_df)
header=T retrieves the title of xlsx file in R.
stringsAsFactors=F is used instead of T as we do not want R to treat the character inputs as factors.
You can find that as the encoding is set to “UTF-8”, so the Chinese characters are shown in UTF code. If encoding is not set, the garbled code is shown.
If you want to display Chinese characters or specific characters in other languages, the locale should be set beforehand. In this example, local = “Chinese” should be specified.
local = “cht” can only display traditional Chinese text while local = “Chinese” can display both Traditional and Simplified Chinese words.
Sys.setlocale(category = "LC_ALL", locale = "Chinese")# read excel xlsx - method 1xlsx_df = read.xlsx(file="Reference_Sample.xlsx", sheetName="Sample_Sheet", header=T, stringsAsFactors=F, encoding="UTF-8")
# read excel xlsx - method 2xlsx_df2 = read.xlsx2(file="Reference_Sample.xlsx", sheetName="Sample_Sheet", header=T, stringsAsFactors=F)
You can find that both methods, i.e. read.xlsx and read.xlsx2, show data in the same format. As the default encoding is “UTF-8”, so if the locale is set properly, i.e. locale = “Chinese”, the same output should be shown. Yet, when you check the structure of xlsx_df2, you can find something different.
str(xlsx_df2)
The structure of all variables become character. It may cause inconvenience if you want to do calculations later.
When using read.xlsx and read.xlsx2, if column names contain space, all the spaces are changed to “.”
Thus, when you do a selection on columns by using column names, you need to use the new names, i.e. “Traditional.Chinese” and “Simplified.Chinese” in this case.
The following section is the demonstration for writing excel output.
# write excel xlsx - method 1write.xlsx(xlsx_df, file="Output1.xlsx", sheetName="Sample_Sheet", row.names=F, showNA=F)
row.names = F is indicated to remove the row index used in R.
showNA = F is used so blanks will remain as blanks instead of being replaced by “NA” in the output file.
However, the column names remain as those shown in R if the column names are not defined again before exporting, so you will still find those “.” in the column names.
# write excel xlsx - method 2write.xlsx2(xlsx_df2, file="Output2.xlsx", sheetName="Sample_Sheet", row.names=F, showNA=F)
The output of using write.xlsx2 shows similar output, but the context of all columns of xlsx_df2 is char, so the output will retain the same structure, and you can see that the numbers on column B become characters.
read.xlsx is recommended as the original data structure is not changed, but you need to define the encoding method. (encoding = “UTF-8” is commonly used.)
For writing an excel file, the output files of both write.xlsx and write.xlsx2 are quite similar, so you can use either one.
One limitation of using xlsx is that it only supports the file with .xlsx extension, if you want to read xls file, readxl can be used (will be talked about in the next article).
You can find other articles of data import and export in R here.
If you are interested to know more tricks and skills, you are welcome to browse our website: https://cydalytics.blogspot.com/
LinkedIn:
Carrie Lo — https://www.linkedin.com/in/carrielsc/
Yeung Wong — https://www.linkedin.com/in/yeungwong/
Data Science Fundamentals (R): Import Data from Excel — readxlData Science Fundamentals (R): Import Data from text files — textreadr & readtextData Visualization Tips (Power BI) — Convert Categorical Variables to Dummy VariablesChinese Word Cloud with Different Shape (Python)Making a Game for Kids to Learn English and Have Fun with Python
Data Science Fundamentals (R): Import Data from Excel — readxl
Data Science Fundamentals (R): Import Data from text files — textreadr & readtext
Data Visualization Tips (Power BI) — Convert Categorical Variables to Dummy Variables
Chinese Word Cloud with Different Shape (Python)
Making a Game for Kids to Learn English and Have Fun with Python