Open In App

DataFrame Row Slice in R

Last Updated : 27 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to Slice row in Dataframe using R Programming Language.

Row slicing in R is a way to access the data frame rows and further use them for operations or methods. The rows can be accessed in any possible order and stored in other vectors or matrices as well. Row slicing is an important operation which is easily supported by R programming language. 

There are various ways to slice data frame rows in R : 

  • Using Numeric Indexing
  • Using Name Indexing
  • Indexing using logical vectors

Method 1. Using Numeric Indexing

Numeric indexing in R can be used to access a single or multiple rows from the data frame. The rows to be accessed can be specified in the square brackets using row indices of the data frame. 

dataframe[i,] where i is the row number of the data frame 

R




# creating a data frame
data_frame = data.frame(col1 = c(1:15),
                        col2 = letters[1:15],
                        col3 = c(0,1,1,1,0,0,0,0,
                                 0,1,1,0,1,1,0))
print("Data Frame")
print(data_frame)
  
# accessing a particular row from
# data frame
data_frame4 <- data_frame[4,]
print("Row 4 of data frame")
print(data_frame4)


Output

[1] "Data Frame"
   col1 col2 col3
1     1    a    0
2     2    b    1
3     3    c    1
4     4    d    1
5     5    e    0
6     6    f    0
7     7    g    0
8     8    h    0
9     9    i    0
10   10    j    1
11   11    k    1
12   12    l    0
13   13    m    1
14   14    n    1
15   15    o    0
[1] "Row 4 of data frame"
  col1 col2 col3
4    4    d    1

Multiple rows of the data frame can also be accessed in the data frame by specifying the row number in the vector and them using them in the square brackets. 

R




# creating a data frame
data_frame = data.frame(col1 = c(1:15),
                        col2 = letters[1:15],
                        col3 = c(0,1,1,1,0,0,0,
                                 0,0,1,1,0,1,1,0))
print("Data Frame")
print(data_frame)
  
# specifying multiple rows of the
# data frame
row_vec <- c(1,3,5)
  
# accessing the rows of the data frame
data_frame_mul <- data_frame[row_vec,]
print("Multiple rows of data frame")
print(data_frame_mul)


Output

[1] "Data Frame"
   col1 col2 col3
1     1    a    0
2     2    b    1
3     3    c    1
4     4    d    1
5     5    e    0
6     6    f    0
7     7    g    0
8     8    h    0
9     9    i    0
10   10    j    1
11   11    k    1
12   12    l    0
13   13    m    1
14   14    n    1
15   15    o    0
[1] "Multiple rows of data frame"
  col1 col2 col3
1    1    a    0
3    3    c    1
5    5    e    0

Method 2. Using Name Indexing

Data frame rows can also be accessed by specifying the names of the rows used for their identification. 

R




# creating a data frame
data_frame = data.frame(col1 = c(1:8),
                        col2 = letters[1:8],
                        col3 = c(0,1,1,1,0,0,0,0))
  
# assigning row names 
rownames(data_frame) <- c("r1","r2","r3","r4",
                          "r5","r6","r7","r8")
print("Data Frame")
print(data_frame)
  
# subjecting to a logical condition 
data_frame_3 = data_frame["r3",]
  
print("DataFrame row 3")
print (data_frame_3)


Output

[1] "Data Frame"
   col1 col2 col3
r1    1    a    0
r2    2    b    1
r3    3    c    1
r4    4    d    1
r5    5    e    0
r6    6    f    0
r7    7    g    0
r8    8    h    0
[1] "DataFrame row 3"
   col1 col2 col3
r3    3    c    1

Method 3. Indexing using logical vectors

Data frame rows can also be subjected to a logical expression, where in the row indexes evaluating to true of the logical expression will be returned in the resulting output. 

R




# creating a data frame
data_frame = data.frame(col1 = c(1:8),
                        col2 = letters[1:8],
                        col3 = c(0,1,1,1,0,0,0,0))
  
# assigning row names 
rownames(data_frame) <- c("r1","r2","r3","r4","r5","r6")
print("Data Frame")
print(data_frame)
  
# accessing rows of data frame
# using a logical condition 
log_vec = data_frame$col3 == 0 
  
print("Dataframe where col3 values are equivalent to 0")
print(data_frame[log_vec,])


Output

[1] "Data Frame"
  col1 col2 col3
1    1    a    0
2    2    b    1
3    3    c    1
4    4    d    1
5    5    e    0
6    6    f    0
7    7    g    0
8    8    h    0
[1] "Dataframe where row 1 values are %2 "
  col1 col2 col3
1    1    a    0
5    5    e    0
6    6    f    0
7    7    g    0
8    8    h    0


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads