Select Odd and Even Rows and Columns from DataFrame in R - GeeksforGeeks

23 Sep, 2021
In this article, we will discuss how to select odd and even rows from a dataframe in R programming language.
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
# creating a data frame in Rdata_frame <- data.frame(col1 = 1:10,                         col2 = letters[1:10],                         col3 = rep(5:9,2))  print ("Original Dataframe")print(data_frame)  # getting number of rows in Rrows <- nrow(data_frame)  # extracting odd rows odd_rows <- seq_len(rows) %% 2  # getting data from odd data framedata_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
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
# creating a data frame in Rdata_frame <- data.frame(col1 = 1:10,                         col2 = letters[1:10],                         col3 = rep(5:9,2))  print ("Original Dataframe")print(data_frame)  # getting number of rows in Rrows <- nrow(data_frame)  # extracting odd rows even_rows <- seq_len(rows) %% 2  # getting data from odd data framedata_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
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
# creating a data frame in Rdata_frame <- data.frame(col1 = 1:10,                         col2 = letters[1:10],                         col3 = rep(5:9,2))  print ("Original Dataframe")print(data_frame)  # getting number of columns in Rcols <- ncol(data_frame)  # extracting odd rows odd_cols <- seq_len(cols) %% 2  # getting data from odd data framedata_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
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
# creating a data frame in Rdata_frame <- data.frame(col1 = 1:10,                         col2 = letters[1:10],                         col3 = rep(5:9,2))  print ("Original Dataframe")print(data_frame)  # getting number of columns in Rcols <- ncol(data_frame)  # extracting odd rows even_cols <- seq_len(cols) %% 2  # getting data from odd data framedata_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"
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R