r - Multiple uses of setdiff() on consecutive groups without for looping -


i setdiff between consecutive groups without looping, if possible datatable way or function of apply family.

dataframe df :

   id group 1  l1     1 2  l2     1 3  l1     2 4  l3     2 5  l4     2 6  l3     3 7  l5     3 8  l6     3 9  l1     4 10 l4     4 11 l2     5 

i want know how new ids there between consecutive groups. so, example, if compare group 1 , 2, there 2 new ids : l3 , l4 returns 2 (not setdiff directly length()), if compare group 2 , 3, l5 , l6 news ids returns 2 , on.

expected results :

new_id   2   2   2   1 

data :

structure(list(id = structure(c(1l, 2l, 1l, 3l, 4l, 3l, 5l, 6l,  1l, 4l, 2l), .label = c("l1", "l2", "l3", "l4", "l5", "l6"), class = "factor"),      group = c(1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5)), class = "data.frame", row.names = c(na,  -11l), .names = c("id", "group")) 

here option mapply:

lst <- with(df, split(id, group))    mapply(function(x, y) length(setdiff(y, x)), head(lst, -1), tail(lst, -1))  #1 2 3 4  #2 2 2 1  

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -