Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Read CSV file and select specific rows and columns in R

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 to set the path to where the CSV file is located using setwd( ) otherwise we can pass the full path of the CSV file into read.csv( ) as a parameter.

Example 1: Selecting specific multiple rows

R




df = read.csv('C:/Users/KRISHNA KARTHIKEYA/Documents/item.csv')
  
a = df[c(2, 7),]
print(a)

Output :

Explanation :

  • In the first step, we have imported a CSV file into the R environment using read.csv( ) function.
  • In the next step, we have selected 2,7 rows from CSV file using indexing and storing the result into a variable

Example 2: Selecting specific single rows

R




df = read.csv('C:/Users/KRISHNA KARTHIKEYA/Documents/item.csv')
a = df[ 2, ]
print(a)

Output :

Example 3: Selecting multiple columns

R




df = read.csv('C:/Users/KRISHNA KARTHIKEYA/Documents/item.csv')
a = df[, c(3 , 4)]
print(a)

Output : 


My Personal Notes arrow_drop_up
Last Updated : 20 May, 2021
Like Article
Save Article
Similar Reads