R: Extract elements common in all column groups -


i have r dataset x below:

  id month 1   1   jan 2   3   jan 3   4   jan 4   6   jan 5   6   jan 6   9   jan 7   2   feb 8   4   feb 9   6   feb 10  8   feb 11  9   feb 12 10   feb 13  1   mar 14  3   mar 15  4   mar 16  6   mar 17  7   mar 18  9   mar 19  2   apr 20  4   apr 21  6   apr 22  7   apr 23  8   apr 24 10   apr 25  1   may 26  2   may 27  4   may 28  6   may 29  7   may 30  8   may 31  2   jun 32  4   jun 33  5   jun 34  6   jun 35  9   jun 36 10   jun 

i trying figure out r function/code identify ids exist atleast once in every month. in above case, ids 4 & 6 present in months.

thanks

first, split df$id month , use intersect find elements common in each sub-group.

reduce(intersect, split(df$id, df$month)) #[1] 4 6 

if want subset corresponding data.frame, do

df[df$id %in% reduce(intersect, split(df$id, df$month)),] 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -