Open In App

How To Use Readxl Package To Read Data In R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article let’s discuss how to use the Readxl Package to read data in the R Programming Language.

Readxl Package in R

The readxl package in R is used to read data from the Excel files, i.e., the format .xls and .xlsx files. The readxl package in R provides a function called read_excel() which is used to read the data from Excel files. The read_excel() method accepts the Excel file which needs to read the content from it. 

Syntax

library(readxl)
data<-read_excel(‘excelFile’,sheet=1)

Parameters

  • excelFile – It specifies the excel file from which we read data.
  • sheet – It specifies from which work sheet we need to read. It is an optional parameter.

Syntax to import and install the readxl package in R

install.package('readxl')
library(readxl)

Here let’s look into how to read the data of the first worksheet from an excel file using the read_excel() method present in the Readxl package.

R




# import readxl package
library(readxl)
 
# reading data from an excel file
data <-read_excel('C:\\Users\\GFG19565\\Downloads\\CardioGoodFitness.xlsx')
print(data)


Output

   Product   Age Gender Education MaritalStatus Usage Fitness Income Miles
<chr> <dbl> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 TM195 18 Male 14 Single 3 4 29562 112
2 TM195 19 Male 15 Single 2 3 31836 75
3 TM195 19 Female 14 Partnered 4 3 30699 66
4 TM195 19 Male 12 Single 3 3 32973 85
5 TM195 20 Male 13 Partnered 4 2 35247 47
6 TM195 20 Female 14 Partnered 3 3 32973 66

The dataset appears to contain information related to fitness products and their users. Each row in the dataset represents a particular individual, and the columns provide various attributes and characteristics associated with them.

Reading Specific Rows using Readxl Package in R

The readxl package in R to read Excel files, and the read_excel() function is commonly used for this purpose. to read specific rows from an Excel file using readxl, we can use the skip and n_max arguments to skip a certain number of rows at the beginning and read a specific number of rows.

R




data1 <- read_excel('C:\\Users\\GFG19565\\Downloads\\CardioGoodFitness.xlsx',skip = 2)
data1


Output:

   TM195  `19` Male    `15` Single      `2`   `3` `31836`  `75`
<chr> <dbl> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 TM195 19 Female 14 Partnered 4 3 30699 66
2 TM195 19 Male 12 Single 3 3 32973 85
3 TM195 20 Male 13 Partnered 4 2 35247 47
4 TM195 20 Female 14 Partnered 3 3 32973 66
5 TM195 21 Female 14 Partnered 3 3 35247 75

read_excel function from the readxl package to read an Excel file named ‘CardioGoodFitness.xlsx’. The skip argument is set to 2, which means you are skipping the first two rows of the Excel file.

After running this command, the data from the Excel file is stored in the data1 variable. You can now explore and analyze the contents of the data1 dataframe in R.

Reading Specific Cells using Readxl Package in R

In the readxl package in R, we can use the read_excel() function to read specific cells from an Excel file.

R




data2 <- read_excel('C:\\Users\\GFG19565\\Downloads\\CardioGoodFitness.xlsx',range = "A2:D6")
data2


Output:

  TM195  `18` Male    `14`
<chr> <dbl> <chr> <dbl>
1 TM195 19 Male 15
2 TM195 19 Female 14
3 TM195 19 Male 12
4 TM195 20 Male 13

read_excel function with the range argument to read a specific range of cells from the Excel file ‘CardioGoodFitness.xlsx’. The specified range is “A2:D6,” indicating you want to read data from columns A to D and rows 2 to 6.



Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads