I have a vector 'a' that is a numerical vector of row indices. I'd like to get these rows as well as the next 20 rows next to them out of a dataframe called 'data'. Is there a way to do this?
I've already tried:
data2 <- data[a:a+20,]
but without success.
If a <- c(1, 21, 42)
I'd like to extract rows 1:20, 21:41, and 42:62 from 'data' and store them in another object.
data2 <- data[-a,]
? Your question isn't very clear. What do you expect the final dataframe to look like? Are you trying to get a single dataframe or a list of three dataframes? An actual example (perhaps using a built-in dataset liketrees
) would help.c(1,21,31)
to1:20, 21:41, 31:41
? For one thing, where did41
come from? Furthermore, wouldn't1:20, 21:41, 31:41
duplicate the rows in31:41
, making the resulting dataframe no longer a subset?data[1:62, ]
? Or if you have a vector,data[min(a):max(a), ]
.