I saw this stack-overflow question already. But what I want is to remove first N rows in my data set. I can't think of a solution as I am new to R.
Asked
Active
Viewed 5.3k times
9
-
Replace the 2 with N in the link and you have your answer. – Rich Scriven Jun 12 '16 at 05:27
2 Answers
24
In this case, we need the opposite, so tail
can be used.
N <- 5
tail(df, -N)
# a
#6 6
#7 7
#8 8
#9 9
#10 10
It can be wrapped in a function and specify a condition to return the full dataset if the value of N
is negative or 0
f1 <- function(dat, n) if(n <= 0) dat else tail(dat, -n)
f1(df, 0)
f1(df, 5)
data
df <- data.frame( a = 1:10 )

akrun
- 874,273
- 37
- 540
- 662