Tabulating in a different manner in R -
this question related sort r rows based on number of repetition
from next info frame:
> ddf aa bb 1 c efg 2 d cde 3 d abc 4 c abc 5 b efg 6 b cde 7 c abc 8 c abc 9 c cde 10 b cde > > > dput(ddf) structure(list(aa = structure(c(2l, 3l, 3l, 2l, 1l, 1l, 2l, 2l, 2l, 1l), .label = c("b", "c", "d"), class = "factor"), bb = structure(c(3l, 2l, 1l, 1l, 3l, 2l, 1l, 1l, 2l, 2l), .label = c("abc", "cde", "efg"), class = "factor")), .names = c("aa", "bb"), row.names = c(na, -10l), class = "data.frame")
i can sort it:
> ddf[order(ddf$bb),] aa bb 3 d abc 4 c abc 7 c abc 8 c abc 2 d cde 6 b cde 9 c cde 10 b cde 1 c efg 5 b efg
and can tabulate following:
> t(with(ddf, table(aa,bb))) aa bb b c d abc 0 3 1 cde 2 1 1 efg 1 1 0
but want have output following:
abc c c c d cde b b c d eft b c
i tried:
ll = list() for(xx in unique(ddf$bb)) { ll[[length(ll)+1]] = xx ll[[length(ll)+1]] = ddf[ddf$bb==xx,]$aa } ll [[1]] [1] "efg" [[2]] [1] c b levels: b c d [[3]] [1] "cde" [[4]] [1] d b c b levels: b c d [[5]] [1] "abc" [[6]] [1] d c c c levels: b c d
but cannot combine these have output like:
abc c c c d cde b b c d eft b c
the b,c,d etc should sorted shown above. help.
edit: works reply provided @richard scriven:
> aggregate(aa ~ bb, ddf, function(x) paste(sort(x))) bb aa 1 abc c, c, c, d 2 cde b, b, c, d 3 efg b, c
but why next (which had tried earlier) gives numbers?
> aggregate(aa ~ bb, ddf, function(x) sort(x)) bb aa 1 abc 2, 2, 2, 3 2 cde 1, 1, 2, 3 3 efg 1, 2
you utilize aggregate
anonymous function sort
paste
values.
aggregate(aa ~ bb, ddf, function(x) paste(sort(x), collapse = " ")) # bb aa # 1 abc c c c d # 2 cde b b c d # 3 efg b c
r
No comments:
Post a Comment