在R中以不同的方式制表

时间:2014-10-09 17:09:42

标签: r

此问题与Sort R rows based on the number of repetition

有关

从以下数据框:

> 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")

我可以对它进行排序:

> 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

我可以列表如下:

> t(with(ddf, table(aa,bb)))
     aa
bb    b c d
  abc 0 3 1
  cde 2 1 1
  efg 1 1 0

但我希望输出如下:

abc  c c c d
cde  b b c d
eft  b c

我试过了:

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

但我无法将这些结合起来,如:

abc  c c c d
cde  b b c d
eft  b c

b,c,d等应如上所示排序。谢谢你的帮助。

编辑: 它适用于@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

但为什么跟随(我之前尝试过的)只提供数字?

> 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

1 个答案:

答案 0 :(得分:2)

您可以aggregate使用匿名函数sort,然后使用paste值。

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