看板 R_Language 關於我們 聯絡資訊
[問題類型]:程式諮詢 用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 [關鍵字]: -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.114.3.90 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1553357382.A.CE2.html
celestialgod: dcast 03/24 00:51
andrew43: res <- xtabs(~ olds + group, DT); res/colSums(res) 03/24 02:05
paganina: 謝謝兩位前輩協助,目前引用了cel大的dcast,有排除問題 03/24 15:53
paganina: 了,感謝大家協助 03/24 15:53