Open In App

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 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




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

Output :

Explanation :

Example 2: Selecting specific single rows




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

Output :

Example 3: Selecting multiple columns




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

Output : 


Article Tags :