user3142384 user3142384 - 2 months ago 5x
R Question

Hierarchical data to be arranged

Hierarchical data is to be arranged.
Imagine that df is given. (Here i generate df with some fake data )

df1 <-data.frame("Col1" = rep("a",8), "Col2"= c(rep("M",3),rep("N",2), rep("O", 2), rep("P",1)), "Col3" = LETTERS[1:8])
df2 <-data.frame("Col1" = rep("b",13), "Col2"= c(rep("p",4),rep("q",5),rep("r",3),rep("s",1)), "Col3" = LETTERS[1:13])
df <- rbind(df1,df2)

For each element of Col1, we have to get a collection in sorted way
Finally, what i look for is a list of lists :
list a : (1,2,2,3)
list b : (1,3,4,5)

ll <- split(df, df$Col1)
lapply(ll, function(dat){
    v <- Filter(function(v) !, with(dat, tapply(Col1, Col2, length)))