我需要改變這個數據集:重塑數據到交叉表
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
,其中一個,b,c,d,e,f,g,h爲頻率(h:2013年C類用戶和2012年B類用戶數)
我試着用dcast,melt,xtabs,但是我沒有達到它。
有什麼想法?
THX
編輯:該解決方案需要無序數據框中工作,並與失蹤行...
類似於這個方法是'table(reshape(DF,direction =「wide」,idvar =「user」,timevar =「year」)[ - 1])' – 2014-09-29 22:16:01