Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Import Only Selected Columns of Data from CSV in R

  • Last Updated : 17 Jun, 2021

In this article, we will be looking at two different approaches to import selected columns of the Data from a CSV file in the R programming language.

Method 1: Using read.table() function

In this method of only importing the selected columns of the CSV file data, the user needs to call the read.table() function, which is an in-built function of R programming language, and then passes the selected column in its arguments to import particular columns from the data. Here, the user has to pass the null value to the parameter, to avoid importing that particular column.

read.table() function reads a file in table format and creates a data frame from it, with cases corresponding to lines and variables to fields in the file.

Syntax:

read.table(file, header, nrows, skip, colClasses, sep)



Parameters:

  • file: Specifies the name of the file.
  • header:The header is a logical flag indicating whether the first line is a header line contains data or not.
  • nrows: Specifies number of rows in the dataset.
  • skip: Helps in skipping of lines from the beginning.
  • colClasses: It is a character vector which indicates class of each column of the data set.
  • sep: It a string indicating the way the columns are separated that is by commas, spaces, colons, tabs etc.

Dataset in Use:

Example:

R




gfg_data <- read.table("gfg_data.csv", header = TRUE, sep = ",",
                           colClasses = c("numeric", "NULL", "NULL",
                                          "numeric", "NULL"))
  
gfg_data

Output:

Method 2: Using fread() function from the data.table package:

In this approach to import only selected columns to the R programming language, the user first needs to install and import the data.table package in the R console and call the read() function which is the function of the data.table package, with the file location and the selected columns which are to be imported in the select argument of this function. Further, this will lead to importing of the selected columns  



fread() function is fast and more convenient to controls such as sep, colClasses, and nrows are automatically detected

Syntax:

fread(file, sep, colClasses, nrows)

Parameter:

  • file: Specifies the name of the file.
  • colClasses: It is a character vector which indicates class of each column of the data set.
  • sep: It a string indicating the way the columns are separated that is by commas, spaces, colons, tabs etc.
  • nrows: Specifies number of rows in the dataset.

Example:

R




library("data.table")   
  
gfg_data <- fread("gfg_data.csv",
                  select = c("A", "C", "E"))
  
gfg_data

Output:
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!