将数据重塑为交叉制表

时间:2014-09-29 14:42:31

标签: r

我需要转换这个数据集:

   user year type
1     1 2012    A
2     2 2012    C
3     3 2012    C
4     4 2012    B
5     5 2012    C
6     6 2012    A
7     7 2012    A
8     8 2012    A
9     9 2012    C
10   10 2012    C
11    1 2013    B
12    2 2013    C
13    3 2013    C
14    4 2013    C
15    5 2013    C
16    6 2013    A
17    7 2013    C
18    8 2013    A
19    9 2013    B
20   10 2013    C

使用dput

DF <-structure(list(user = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"), 
year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2012", 
"2013"), class = "factor"), type = structure(c(1L, 3L, 3L, 
2L, 3L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 3L, 1L, 
2L, 3L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("user", 
"year", "type"), row.names = c(NA, -20L), class = "data.frame")

我想以这种方式交叉制表

    A(2012) B(2012) C(2012)
A(2013) a   b   c
B(2013) d   e   f
C(2013) g   h   i

其中a,b,c,d,e,f,g,h是频率(h:2013年类型为C且2012年为B类的用户数)

我尝试使用dcast,融化,xtabs,但我没有实现它。

有什么想法吗?

thx

编辑:解决方案需要使用无序数据帧,并且缺少行...

2 个答案:

答案 0 :(得分:3)

另一个类似的想法:

do.call(table, split(DF$type, DF$year))
#    2013
#2012 A B C
#   A 2 1 1
#   B 0 0 1
#   C 0 1 4

看起来table可以方便地处理“列表”参数,因此table(split(DF$type, DF$year))也适用。

答案 1 :(得分:1)

这是我找到的解决方案。 我认为这是最有效的:

library(reshape2)    
with(dcast(DF,user~year),table(`2012`,`2013`))
相关问题