数个维度

时间:2013-08-16 13:17:17

标签: r

是否有一个函数table或xtabs可以通过以下方式计算Titanic数据:

我不需要像

那样分成“类”
df<-data.frame(Titanic)
tapply(df$Freq,list(df$Sex,df$Age,df$Survived),sum)

但输出应如下所示:“new_output”=

1               board_Crew             board_Crew_Male 862
2               board_Crew           board_Crew_Female  23
3          board_Crew_Male       board_Crew_Male_Child   0
4        board_Crew_Female     board_Crew_Female_Child   0
5          board_Crew_Male       board_Crew_Male_Adult 862
6        board_Crew_Female     board_Crew_Female_Adult  23
7    board_Crew_Male_Child    board_Crew_Male_Child_No   0
8  board_Crew_Female_Child  board_Crew_Female_Child_No   0
9    board_Crew_Male_Adult    board_Crew_Male_Adult_No 670
10 board_Crew_Female_Adult  board_Crew_Female_Adult_No   3
11   board_Crew_Male_Child   board_Crew_Male_Child_Yes   0
12 board_Crew_Female_Child board_Crew_Female_Child_Yes   0
13   board_Crew_Male_Adult   board_Crew_Male_Adult_Yes 192
14 board_Crew_Female_Adult board_Crew_Female_Adult_Yes  20

因为在最后一个数据上我可以构建一个图表

g <- graph.data.frame(new_output, directed=TRUE)
plot(g,layout=layout.reingold.tilford(g,root=1),edge.arrow.size=0.5)

enter image description here

2 个答案:

答案 0 :(得分:4)

也许你正在寻找这个:

df_Crew <- df[df$Class=="Crew",]
L <- lapply(1:4, function(i) aggregate(df_Crew$Freq, by=df_Crew[1:i], sum))
L2 <- lapply(L, function(d) data.frame(group=do.call(paste, c(as.list(d[names(d)!="x"]), sep="_")), freq=d$x))
Reduce(rbind, L2)

结果:

                   group freq
1                   Crew  885
2              Crew_Male  862
3            Crew_Female   23
4        Crew_Male_Child    0
5      Crew_Female_Child    0
6        Crew_Male_Adult  862
7      Crew_Female_Adult   23
8     Crew_Male_Child_No    0
9   Crew_Female_Child_No    0
10    Crew_Male_Adult_No  670
11  Crew_Female_Adult_No    3
12   Crew_Male_Child_Yes    0
13 Crew_Female_Child_Yes    0
14   Crew_Male_Adult_Yes  192
15 Crew_Female_Adult_Yes   20

答案 1 :(得分:2)

这是使用plyr

的选项
library(plyr)
margins <- Reduce(c,names(df)[1:4],accumulate=T)

ldply(margins, function(x) ddply(df,x,summarise,count=sum(Freq)))
   Class count    Sex   Age Survived
1    1st   325   <NA>  <NA>     <NA>
2    2nd   285   <NA>  <NA>     <NA>
3    3rd   706   <NA>  <NA>     <NA>
4   Crew   885   <NA>  <NA>     <NA>
5    1st   180   Male  <NA>     <NA>
6    1st   145 Female  <NA>     <NA>
7    2nd   179   Male  <NA>     <NA>
8    2nd   106 Female  <NA>     <NA>
9    3rd   510   Male  <NA>     <NA>
10   3rd   196 Female  <NA>     <NA>
11  Crew   862   Male  <NA>     <NA>
12  Crew    23 Female  <NA>     <NA>
13   1st     5   Male Child     <NA>
14   1st   175   Male Adult     <NA>
15   1st     1 Female Child     <NA>
16   1st   144 Female Adult     <NA>
17   2nd    11   Male Child     <NA>
18   2nd   168   Male Adult     <NA>
19   2nd    13 Female Child     <NA>
20   2nd    93 Female Adult     <NA>
21   3rd    48   Male Child     <NA>
22   3rd   462   Male Adult     <NA>
23   3rd    31 Female Child     <NA>
24   3rd   165 Female Adult     <NA>
25  Crew     0   Male Child     <NA>
26  Crew   862   Male Adult     <NA>
27  Crew     0 Female Child     <NA>
28  Crew    23 Female Adult     <NA>
29   1st     0   Male Child       No
30   1st     5   Male Child      Yes
31   1st   118   Male Adult       No
32   1st    57   Male Adult      Yes
33   1st     0 Female Child       No
34   1st     1 Female Child      Yes
35   1st     4 Female Adult       No
36   1st   140 Female Adult      Yes
37   2nd     0   Male Child       No
38   2nd    11   Male Child      Yes
39   2nd   154   Male Adult       No
40   2nd    14   Male Adult      Yes
41   2nd     0 Female Child       No
42   2nd    13 Female Child      Yes
43   2nd    13 Female Adult       No
44   2nd    80 Female Adult      Yes
45   3rd    35   Male Child       No
46   3rd    13   Male Child      Yes
47   3rd   387   Male Adult       No
48   3rd    75   Male Adult      Yes
49   3rd    17 Female Child       No
50   3rd    14 Female Child      Yes
51   3rd    89 Female Adult       No
52   3rd    76 Female Adult      Yes
53  Crew     0   Male Child       No
54  Crew     0   Male Child      Yes
55  Crew   670   Male Adult       No
56  Crew   192   Male Adult      Yes
57  Crew     0 Female Child       No
58  Crew     0 Female Child      Yes
59  Crew     3 Female Adult       No
60  Crew    20 Female Adult      Yes