9

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.

Community
  • 1
  • 1
Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57

2 Answers2

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
8

Based on the df used in the example above:

N <- 5

df[-(1:N), , drop = FALSE]
milan
  • 4,782
  • 2
  • 21
  • 39