Method 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
DataFrame Row Slice in R
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.