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
Similar Reads
DataFrame Rows & Column Segment in R The process of extracting the row and column information in a dataset by simply using the index or slice operator is known as Slicing. In R Programming, the rows and columns can be sliced in two ways either by using an index or using the name of the row and column. The slice operator is much more us
2 min read
List of Dataframes in R DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types. In this article, we will study h
7 min read
Slice() From Dplyr In R With so much data around us in today's world, dealing with them becomes tough. In this case, the Dplyr data frame package from R acts as a lifesaver and that package stands out as a powerful and versatile tool. for data manipulation. In R Programming Language package has many functions and among the
11 min read
Indexing and Slicing Data Frames in R Indexing and Slicing are use for accessing and manipulating data.Indexing: Accessing specific elements (rows or columns) in data structures.Slicing: Extracting subsets of data based on conditions or indices.In R, indexing a data frame allows you to retrieve specific columns by their names:dataframeN
3 min read
Tibbles Dataframe A tibble is a more advanced data frame in R, providing a cleaner, more consistent way of handling data. It is contained within the tidyverse package and provides a better way to handle large datasets, with a cleaner, easier-to-read output. Tibbles will only display a subset of columns and rows when
3 min read