The Wayback Machine - https://web.archive.org/web/20240721000648/https://www.geeksforgeeks.org/how-to-select-dataframe-columns-by-index-in-r/
Open In App

How to Select DataFrame Columns by Index in R?

Last Updated : 12 Dec, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In this article, we will discuss how to select columns by index from a dataframe in R programming language.

Note: The indexing of the columns in the R programming language always starts from 1.

Method 1: Select Specific Columns By Index with Base R

Here, we are going to select columns by using index with the base R in the dataframe.

Syntax:

dataframe[,c(column_indexes)]

Example:

R




#  create a dataframe with 3 rows and 4 columns
data=data.frame(name=c("akash","kyathi","preethi"),
                subjects=c("java","R","dbms"),
                marks=c(90,98,78))
 
# select second and third column
print(data[,c(2,3)])


Output:

  subjects marks
1     java    90
2        R    98
3     dbms    78

Method 2: Select Specific Columns In The Index Range

We can select by using the index range operator, as shown below.

Syntax:

dataframe[,column_index_start:column_index_end]

where.

  • column_index_start is the first index number, and column_index_end is the second index number.

Example:

R




# create a dataframe with 3 rows and 4 columns
data=data.frame(name=c("akash","kyathi","preethi"),
                subjects=c("java","R","dbms"),
                marks=c(90,98,78))
 
# select first to third column
print(data[,1:3])


Output:

     name subjects marks
1   akash     java    90
2  kyathi        R    98
3 preethi     dbms    78

Method 3: Select Index Column By Excluding Columns Indexes

We can exclude the index columns by specifying in the c() vector with a – sign.

Syntax:

dataframe[,-c(column_indexes)]

Example:

R




# create a dataframe with 3 rows and 4 columns
data=data.frame(name=c("akash","kyathi","preethi"),
                subjects=c("java","R","dbms"),
                marks=c(90,98,78))
 
# exclude second and third column
print(data[,-c(2,3)])


Output:

[1] "akash"   "kyathi"  "preethi"

Method 4: Select Column Names By Index Using dplyr

The select() function from the dplyr package is used for selecting column by index.

Syntax:

dataframe %>%
 select(column_numbers)

where

  • %>%  operator is to load into dataframe

Syntax to import and install the dplyr package:

install.packages("dplyr")
library("dplyr")

Example:

R




# loads the package
library("dplyr")
 
# create a dataframe with 3 rows and 4 columns
data = data.frame(name=c("akash", "kyathi", "preethi"),
                  subjects=c("java", "R", "dbms"),
                  marks=c(90, 98, 78))
 
# select 1  and 3 columns
data % >%
select(1, 3)


Output:

     name marks
1   akash    90
2  kyathi    98
3 preethi    78


Similar Reads

How to select multiple DataFrame columns by name in R ?
In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Syntax: list(dataframe_name$column1,dataframe_name$c
1 min read
Select Only Numeric Columns from DataFrame in R
In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric columns. Syntax: select_if(dataframe, is.numeric) wher
2 min read
Select Odd and Even Rows and Columns from DataFrame in R
In this article, we will discuss how to select odd and even rows from a dataframe in R programming language. Getting Odd Rows from the Data Frame The number of rows in a data frame in R can be fetched by the nrow() method. It returns the number of rows in the data frame. The seq_len() method is then applied to generate the integers beginning with 1
6 min read
How to Select Specific Columns in R dataframe?
In this article, we will discuss how to select specific columns from a data frame in the R Programming Language. Selecting specific Columns Using Base R by column nameIn this approach to select a specific column, the user needs to write the name of the column name in the square bracket with the name of the given data frame as per the requirement to
7 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 <- data.frame( name = c("Abhi", "Bhavesh
2 min read
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 u
1 min read
How to read Excel file and select specific rows and columns in R?
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_inde
2 min read
Select variables (columns) in R using Dplyr
In this article, we are going to select variables or columns in R programming language using dplyr library. Dataset in use: Select column with column name Here we will use select() method to select column by its name Syntax: select(dataframe,column1,column2,.,column n) Here, data frame is the input dataframe and columns are the columns in the dataf
5 min read
Select Subset of DataTable Columns in R
In this article, we will discuss how to select a subset of data table columns in R programming language. Let's create a data table using a matrix. First, we need to load data.table package in the working space. Installation install.packages("data.table") Loading library("data.table") Dataset in use: Method 1: Using [] We can select a subset of data
2 min read
Select rows from R DataFrame that contain both positive and negative values
In this article, we will be discussing how to select rows in the data frame that contains both positive and negative values in R Programming Language. Let's take an example for better understanding. Suppose you have the following data frame in R that contains multiple columns and rows. All rows contain negative values or positive values or maybe bo
2 min read
three90RightbarBannerImg