Select Odd and Even Rows and Columns from DataFrame in R
Last Updated :
23 Sep, 2021
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 to the number of rows. The modulo method with the integer 2 in R can be used to fetch the odd or even rows based on their indexes on the obtained vector. The corresponding row indices of the data frame are operated with a modulo method. The following syntax is used to fetch the odd rows of the data frame.
Syntax:
seq_len(rows)%%2
Data frame indexing method can then be used to obtain rows where the odd row index is equivalent to 1.
Syntax:
data_frame[odd_row == 1, ]
Example: Select odd rows from dataframe
R
data_frame <- data.frame (col1 = 1:10,
col2 = letters [1:10],
col3 = rep (5:9,2))
print ( "Original Dataframe" )
print (data_frame)
rows <- nrow (data_frame)
odd_rows <- seq_len (rows) %% 2
data_mod <- data_frame[odd_rows == 1, ]
print ( "odd rows of dataframe" )
print (data_mod)
|
Output:
[1] "Original Dataframe"
col1 col2 col3
1 1 a 5
2 2 b 6
3 3 c 7
4 4 d 8
5 5 e 9
6 6 f 5
7 7 g 6
8 8 h 7
9 9 i 8
10 10 j 9
[1] "odd rows of dataframe"
col1 col2 col3
1 1 a 5
3 3 c 7
5 5 e 9
7 7 g 6
9 9 i 8
Getting Even 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 to the number of rows. The modulo method with the integer 2 in R can be used to fetch the odd or even rows based on their indexes on the obtained vector. The corresponding row indices of the data frame are operated with a modulo method. The following syntax is used to fetch the odd rows of the data frame.
Syntax:
seq_len(rows)%%2
Data frame indexing method can then be used to obtain rows where the even row index is equivalent to 0.
Syntax:
data_frame[odd_row == 0, ]
Example: Select even rows from dataframe
R
data_frame <- data.frame (col1 = 1:10,
col2 = letters [1:10],
col3 = rep (5:9,2))
print ( "Original Dataframe" )
print (data_frame)
rows <- nrow (data_frame)
even_rows <- seq_len (rows) %% 2
data_mod <- data_frame[even_rows == 0, ]
print ( "even rows of dataframe" )
print (data_mod)
|
Output:
[1] "Original Dataframe"
col1 col2 col3
1 1 a 5
2 2 b 6
3 3 c 7
4 4 d 8
5 5 e 9
6 6 f 5
7 7 g 6
8 8 h 7
9 9 i 8
10 10 j 9
[1] "even rows of dataframe"
> print(data_mod)
col1 col2 col3
2 2 b 6
4 4 d 8
6 6 f 5
8 8 h 7
10 10 j 9
Getting Odd Columns from the Data Frame
The number of columns in a data frame in R can be fetched by the ncol() method. It returns the number of columns in the data frame. The modulo method can then be used with the integer 2 in R can be used to fetch the odd or even columns based on their indexes on the obtained vector. The corresponding column indices of the data frame are operated with a modulo method. The following syntax is used to fetch the odd rows of the data frame.
Syntax:
seq_len(cols)%%2
Data frame indexing method can then be used to obtain rows where the odd column index is equivalent to 1.
Syntax:
Data_frame[, odd_cols==1 ]
Example: select odd columns from dataframe
R
data_frame <- data.frame (col1 = 1:10,
col2 = letters [1:10],
col3 = rep (5:9,2))
print ( "Original Dataframe" )
print (data_frame)
cols <- ncol (data_frame)
odd_cols <- seq_len (cols) %% 2
data_mod <- data_frame[, odd_cols == 1]
print ( "odd columns of dataframe" )
print (data_mod)
|
Output:
[1] "Original Dataframe"
col1 col2 col3
1 1 a 5
2 2 b 6
3 3 c 7
4 4 d 8
5 5 e 9
6 6 f 5
7 7 g 6
8 8 h 7
9 9 i 8
10 10 j 9
[1] "odd rows of dataframe"
col1 col3
1 1 5
2 2 6
3 3 7
4 4 8
5 5 9
6 6 5
7 7 6
8 8 7
9 9 8
10 10 9
Getting Even Columns from the Data Frame
The number of columns in a data frame in R can be fetched by the ncol() method. It returns the number of columns in the data frame. The modulo method can then be used with the integer 2 in R can be used to fetch the odd or even columns based on their indexes on the obtained vector. The corresponding column indices of the data frame are operated with a modulo method. The following syntax is used to fetch the odd rows of the data frame.
Syntax:
seq_len(cols)%%2
Data frame indexing method can then be used to obtain rows where the odd column index is equivalent to 0.
Syntax:
Data_frame[, odd_cols==0 ]
Example: select even columns from dataframe
R
data_frame <- data.frame (col1 = 1:10,
col2 = letters [1:10],
col3 = rep (5:9,2))
print ( "Original Dataframe" )
print (data_frame)
cols <- ncol (data_frame)
even_cols <- seq_len (cols) %% 2
data_mod <- data_frame[, even_cols == 0]
print ( "even columns of dataframe" )
print (data_mod)
|
Output:
[1] "Original Dataframe"
col1 col2 col3
1 1 a 5
2 2 b 6
3 3 c 7
4 4 d 8
5 5 e 9
6 6 f 5
7 7 g 6
8 8 h 7
9 9 i 8
10 10 j 9
[1] "even columns of dataframe"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
Similar Reads
Extract given rows and columns from a given dataframe in R
Extraction of given rows and columns has always been one of the most important tasks which are especially required while working on data cleaning activities. In this article, we will be discussing all the sets of commands which are used to extract given rows and columns from a given dataframe in the
4 min read
Find columns and rows with NA in R DataFrame
A data frame comprises cells, called data elements arranged in the form of a table of rows and columns. A data frame can have data elements belonging to different data types as well as missing values, denoted by NA. Approach Declare data frameUse function to get values to get NA valuesStore positio
3 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 colu
2 min read
How to Add an Empty Column to DataFrame in R?
In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe  is the inpu
1 min read
How to Select DataFrame Columns by Index in R?
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
2 min read
Convert dataframe rows and columns to vector in R
In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met
2 min read
Select rows from a DataFrame based on values in a vector in R
In this article, we will discuss how to select rows from a DataFrame based on values in a vector in R Programming Language. Method 1: Using %in% operator %in% operator in R, is used to identify if an element belongs to a vector or Dataframe. It is used to perform a selection of the elements satisfyi
5 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all componen
3 min read
How to calculate the mode of all rows or columns from a dataframe in R ?
In this article, we will discuss how to calculate the mode of all rows and columns from a dataframe in R Programming Language. Method 1: Using DescTools package The DescTools package in R is used to perform descriptive analysis. It contains a collection of miscellaneous basic statistic functions and
4 min read
Select DataFrame Rows where Column Values are in Range in R
In this article, we will discuss how to select dataframe rows where column values are in a range in R programming language. Data frame indexing can be used to extract rows or columns from the dataframe. The condition can be applied to the specific columns of the dataframe and combined using the logi
2 min read