No matter how much technology has improved over the years for storing and sharing data, one format is still widely used in the industry: Excelsheet, which has file extensions like “.xls” or “.xlsx.“
Here are three ways to import Excel files in R:
For this tutorial’s practical, we will import and read the below Excel file:
The most efficient way to import and read external Excel sheets into R is using the readxl package’s read_excel() method. It reads xls and xlsx files and detects the format from the extension.
Here is the step-by-step guide:
You will need to install this package first if you haven’t already done so.
To install the readxl package:
install.packages("readxl")
Before using the package’s functions, we must import it using the library() function.
library(readxl)
read_excel("path/to/your/file.xlsx")
It is time to pass the external data source Excel file “simple_data_frame.xlsx” to the read_excel() function. This function will return a tibble, an enhanced version of the data frame.
# Load the readxl package
library(readxl)
# Read an Excel file into a data frame
my_data <- read_excel("simple_data_frame.xlsx")
# Display the first few rows of the data
head(my_data)
Output
We got the 3X3 tibble in the output, and the data is the same as the Excel file. Ensure that you don’t lose any rows or columns while you are importing.
If you want to import, read, and modify Excel files in the R environment, install the “xlsx” package and use the read_xlsx() function.
The read_xlsx() function accepts an “xlsx” file and converts it into a Data Frame, which we can modify further to get the desired output.
library(xlsx)
df <- read.xlsx("simple_data_frame.xlsx", sheetIndex = 1) # Read the first sheet
print(df)
If you are using RStudio, you can also use the built-in menu options to import Excel files:
Go to the menu bar at the top and click on File -> Import Dataset -> From Excel.
A window will pop up. Navigate to the Excel file you wish to import and select it.
You’ll see a preview of the data. If it looks correct, click the Import
button to import the data into R.
You can see that we successfully imported data into the data frame using RStudio’s Import Dataset tool.
That’s all!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Before executing an operation on an object, it is advisable to check its length, as…
Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…
Whether you want to add new data to your existing datasets or create new variables…
The square root of a number is a value that is multiplied by itself, giving…
Duplicate rows refer to all the values across all columns that are the same in…
A vector is a data structure that holds the same type of data. When working…