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.
To remove first few rows from each group in R, we can use slice function of dplyr package after grouping with group_by function.
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.
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)
df <- data.frame( a = 1:10 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With