Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How to read excel file in R ?
Next article icon

How to read Excel file and select specific rows and columns in R?

Last Updated : 20 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss how to read an Excel file and select specific rows and columns from it using R Programming Language.

File Used:

To read an Excel file into R we have to pass its path as an argument to read_excel() function readxl library.

Syntax:

read_excel(path)

To select a specific column we can use indexing.

Syntax:

df [ row_index , column_index ]

Here df represents data frame name or Excel file name or anything

Extracting specific rows from Excel file 

For this, we have to pass the index of the row to be extracted as input to the indexing. As a result, the row at the provided index will be fetched and displayed.

Example 1 :

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[5,] print(a) 

Output :

Example 2 :

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[6,] print(a) 

Output :

Example 3 :

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[7,] print(a) 

Output :

To get multiple rows similarly not much modification is required. The index of the rows to be extracted should be passed as a vector to the row_index part of indexing syntax.

Example 4 : 

R
library("readxl")  df=read_excel("C:/users/KRISHNA KARTHIKEYA/Documents/OSWT1.xlsx") print(df[c(2,3),]) 

Output :

Extracting specific columns from Excel file 

This is similar to the approach followed above except that to extract the column index of the column needs to be given as an argument.

Example 1 :

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[,2] print(a) 

Output :

Example 2:

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[,3] print(a) 

Output : 

Example 3 :

R
library(readxl)  setwd('C:/Users/KRISHNA KARTHIKEYA/Documents') df=read_excel('OSWT1.xlsx')  a=df[,4] print(a) 

Output :

To get multiple columns at once the index of the columns to be extracted should be given as a vector in column_index part of the indexing syntax. All the columns with the index provided will be fetched and displayed.

Example 4 :

R
library("readxl")  df=read_excel("C:/users/KRISHNA KARTHIKEYA/Documents/OSWT1.xlsx")  print(df[,c(2,3)]) 

Output : 


Next Article
How to read excel file in R ?
author
krishnakarthikeyakhandrika
Improve
Article Tags :
  • R Language
  • R-Excel

Similar Reads

  • Read CSV file and select specific rows and columns in R
    In this article, we are going to see how to read CSV file and select specific rows and columns in R Programming Language. CSV file: To import a CSV file into the R environment we need to use a pre-defined function called read.csv(). Pass filename.csv as a parameter within quotations. First, we need
    1 min read
  • How To Retrieve A Specific Element In A Csv File?
    A CSV (comma-separated values) file is a text file with comma-separated values. They are used in data science and machine learning to represent the dataset. They are used in business applications to manage large amounts of data. Information in the CSV file can be represented in a tabular form with r
    4 min read
  • Choose Specific Columns of a Data Frame in R Programming - select() Function
    select() function in R Language is used to choose whether a column of the data frame is selected or not. Syntax: select(x, expr) Parameters: x: Data frame expr: condition for selection Example 1: # R program to select specific columns # Loading library library(dplyr) # Create a data frame d <- da
    2 min read
  • How to read column names and metadata from feather files in R arrow?
    To access column names and metadata from feather files in R programming, we use the R Arrow package to load the feather file into a data frame. After loading the feather file, we can retrieve the column names and metadata attributes by using the Feather package.  What is Arrow Package in R? The Arro
    3 min read
  • How to read excel file in R ?
    We may work with structured data from spreadsheets, take advantage of R's capabilities for data analysis and manipulation, and incorporate Excel data into other R processes and packages by reading Excel files in R. The readxl package offers a simple and effective method for reading Excel files into
    3 min read
  • How to Delete Filtered Rows in Excel: Step by Step Guide
    Efficiently managing data in Excel often involves filtering and removing unnecessary rows. However, deleting filtered rows in Excel can be tricky if not done correctly, as unwanted data may get removed. This article provides a step-by-step guide to help you safely and accurately delete filtered rows
    9 min read
  • Specify Row Names when Reading Excel File in R
    In this article, we are going to specify the row names when reading Excel file in the R Programming language. Specifying row names when reading a file using row.names argument of the read.xlsx2() function. This is the easiest approach to specify the row names when reading a file in the R programming
    2 min read
  • How to Convert a CSV File to Microsoft Excel in R
    CSV refers to Comma-Separated Values. It holds plain text as a series of values (cells) separated by commas (, ) in a series of lines (rows). CSV file can actually open in a text editor and read it.  On the other hand, Excel is used to display the data in horizontal and vertical rows. The data are u
    2 min read
  • How to Read an Excel File using polars
    The Polars is a fast, efficient DataFrame library in Python, designed for processing large datasets with low memory usage and high performance. While Polars is more commonly used with CSV, Parquet, and JSON files, we can also work with Excel files, though this requires an additional setup as Polars
    4 min read
  • How to write in a specific Row or column Using write.xlsx in R
    The Excel File contains cells where the data can be stored in them. To deal with Excel Files in R programming Language we have a package named xlsx. Using the write.xlsx() function present in that package we are going to insert data into an excel file. We are going to insert data at a specified row
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences