Reputation: 187
I have a dataset of 50 rows and I'm trying to get the data in rows 5-10, 12 and 21-23. Is there any way to do this in a single line?
Upvotes: 2
Views: 730
Reputation: 71580
Or in base R you could use rbind
:
rbind(df[5:10,], df[12,], df[21:23,])
Upvotes: 2
Reputation: 8811
A tidyverse
approach
library(dplyr)
index_range <- c(5:10, 12, 21:23)
df %>%
slice(index_range)
Upvotes: 1
Reputation: 887301
Yes, just concatenate into a vector
and use it as row index - for those sequence in range, use :
df1[c(5:10, 12, 21:23),]
Upvotes: 4