Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first N rows in a data set in R? [duplicate]

Tags:

r

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.

like image 768
Lasitha Yapa Avatar asked Jun 12 '16 05:06

Lasitha Yapa


People also ask

How do I remove the first n rows in R?

To remove first few rows from each group in R, we can use slice function of dplyr package after grouping with group_by function.

How do I delete multiple rows in dataset in R?

Delete Multiple Rows from R DataframeUse -c() with the row id you wanted to delete, Using this we can delete multiple rows at a time from the R data frame.


1 Answers

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 )
like image 60
akrun Avatar answered Nov 15 '22 09:11

akrun