Grouping by a column in R -


i have data in following format :

 routeid, stoporder, stoptype  101, 1, load  101, 2, unload 102, 1, load 102, 2, load 102, 3, unload 102, 4, unload 103, 1, load 103, 2, unload 103, 3, load 103, 4, unload 

given data, want identify such routeids have load stop after unload stop.

 expected output: 103 

we can try data.table. convert 'data.frame' 'data.table' (setdt(df2), grouped 'routeid', if any of run-length-type id of logical vector (stoptype=='load') greater 2, subset of data.table (.sd). give rows '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 need 'routeid', extract it, subsetting logical vector.

setdt(df2)[, .grp[any(rleid(stoptype=='load') >2)] ,    .(routeid)]$routeid #[1] 103 

or base r option 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" 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -