简体   繁体   中英

Aggregating a dataframe by a group of columns passed as arguments

I have a dataframe which contains feature set for a person at a different times of the day. I want to aggregate it for different durations of time ie 1 hr, 2 hr, and so on. I have written this function as follows.

 library(data.table)

 getAggregate <- function(comb,limb) {
      comb[Limb == as.character(limb),
      c(fb(BC),
      structure(lapply(.SD,mean),names=meanFeatures),
      structure(lapply(.SD,ivar),names=varFeatures)
      )
     ,by=.(Person,hour(Date),yday(Date),wday(Date))
     ,.SDcols = sigfeats]
 }

I just want to pass "Person,hour(Date),yday(Date),wday(Date)" as arguments to the function just like "limb" so that it can aggregate using these four values (or any other values incase the arguments are different).

Maybe this would work like this...

library(lubridate)
library(data.table)

getAggregate <- function(comb, limb, byList) {
    comb$Hour <- hour(comb$Date)
    comb$YDay <- yday(comb$Date)
    comb$WDay <- wday(comb$Date)
    comb$Minute <- minute(Date)

    comb[Limb == as.character(limb),
         c(
             fb(BC),
             structure(lapply(.SD, mean), names = meanFeatures),
             structure(lapply(.SD, ivar), names = varFeatures)
         )
         , by = byList
         , .SDcols = sigfeats]
}

...and you would call the function like so...

getAggregate(comb, limb, byList = c("Person", "Hour", "YDay", "WDay", "Minute"))  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM