How to take range of rows from a dataframe

I have a dataframe containing 100 rows. If I want to extract the first 10 rows in one dataframe and the rest of them in another dataframe. So the code is

df1 <- df[1:10, ]
df2 <- df[11:100, ]

But, if I want to extract the rows from 11 to 20 in one dataframe and the rest of the rows (1-10 and 21-100) in another dataframe then how I have to change the code?

df1 <- df[11:20, ]
df2 <-

sample dataframe

df <- data.frame(items=sample(LETTERS,100,replace=TRUE),

c() is the standard way of collating things of the same type together.

df2 <-df[c(1:10,21:100), ]

You can just add negative signs to your subset.
df[-11:-20, ]

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.