![]() |
VOOZH | about |
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.
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
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.
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
Data frame rows can also be accessed by specifying the names of the rows used for their identification.
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
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.
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