Home > Back-end >  How do I return multiple rows from multiple specific index ranges?
How do I return multiple rows from multiple specific index ranges?

Time:10-13

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?

CodePudding user response:

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),]

CodePudding user response:

A tidyverse approach

library(dplyr)

index_range <- c(5:10, 12, 21:23)

df %>% 
  slice(index_range)

CodePudding user response:

Or in base R you could use rbind:

rbind(df[5:10,], df[12,], df[21:23,])
  •  Tags:  
  • r
  • Related