rdataframedifference-between-rows

Dropping out the last rows in data frame


I have a data frame which could be approximated by the following example df:

a  <- seq(1, 1010, 1)
b  <- seq(2,1011,1)
c  <- c(rep(1,253), rep(2, 252), rep(3,254), rep(4,251))
d  <- c(rep(5,253), rep(6, 252), rep(7,254), rep(8,251))
df <- data.frame(a,b,c,d)

Firstly I group my observations based on columns c and d. Then I want to have equal amount of observations (n=250) in each group. Basically, I want to remove the last rows of each group if they exceed the threshold of 250.

It is pretty easy to do with if, however it does take a plenty of time. Any help will be highly appreciated.


Solution

  • An example using package plyr:

    library(plyr)
    ddply(df, .(c, d), function(DF) head(DF, 250))