[問題類型]:程式諮詢
用R做樞紐分析的表格
[熟悉程度]
入門(寫過一點點程式,對語法不熟悉)
[問題敘述]:
以下是我的資料
group sex olds
1 A 2 0-20
2 A 2 0-20
3 A 2 0-20
4 A 2 0-20
5 A 1 0-20
6 A 2 0-20
7 A 2 0-20
8 A 2 0-20
9 A 1 0-20
10 A 1 0-20
11 A 1 21-60
12 A 2 21-60
13 A 2 21-60
14 A 1 21-60
15 A 2 21-60
16 B 1 21-60
17 B 1 21-60
18 B 1 21-60
19 B 2 21-60
20 B 2 21-60
21 B 1 61-100
22 B 1 61-100
23 B 1 61-100
24 B 2 61-100
25 B 1 61-100
26 B 1 61-100
27 B 1 61-100
28 B 1 61-100
29 B 1 61-100
30 B 2 61-100
想將資料轉成性別(sex)比例的分布
大概會是這樣
group_A group_B
0-20 0.68 0.53
21-60 0.11 0.22
61-100 0.21 0.25
小計 1 1
[目前程式碼]:
https://ideone.com/fork/c3ippk
[環境]
R R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] dplyr_0.8.0.1 lubridate_1.7.4 ggplot2_3.1.0
[4] lattice_0.20-38
[關鍵字]: