[Solved] Grouping by a column in R [closed]


We can try data.table. Convert the ‘data.frame’ to ‘data.table’ (setDT(df2), grouped by ‘RouteId’, if any of the run-length-type id of the logical vector (StopType=='Load') is greater than 2, we get the Subset of Data.table (.SD). This will give the rows with ‘RouteId’ 103.

library(data.table)
setDT(df2)[,if(any(rleid(StopType=='Load') >2)) .SD ,.(RouteId)]
#    RouteId StopOrder StopType
#1:     103         1     Load
#2:     103         2   Unload
#3:     103         3     Load
#4:     103         4   Unload

If we need only the ‘RouteId’, just extract it, by subsetting from the logical vector.

setDT(df2)[, .GRP[any(rleid(StopType=='Load') >2)] ,
   .(RouteId)]$RouteId
#[1] 103

Or a base R option would be

 v1 <-  with(df2, tapply(StopType=='Load', RouteId, 
             FUN= function(x) {i1 <- which(x)
                  i1>1 || any(diff(i1)>1)}))

 names(v1)[v1]
 #[1] "103"

9

solved Grouping by a column in R [closed]