按RE中的条件枢轴和条件枢轴在R中

发布于 2025-01-21 22:08:54 字数 1507 浏览 0 评论 0原文

嗨,我正在尝试按R中的R进行枢轴总和。在我的dataFrame(my_df)下方

my_df <- data.frame(Municipality=c('a', 'a', 'a', 'a', 'a', 'a', 'a','a','b','b'),
                 section=c(1, 1, 1, 1, 1, 1, 2,2,1,1),
                 state=c('ac', 'ac', 'ac', 'ac', 'ac', 'ac', 'ac','ac','mg','mg'),
                 gender=c('male', 'male', 'female', 'male', 'female', 'female', 
                          'male','female','female','female'),
                 age=c('60-64', '17', '18', '18', '21-24', '30-34', '19','40-44','60-64','50-54'),
                 age_code=c('6064', '1700', '1800', '1800', '2124', '3034', 
                            '1900','4044','6064','5054'),
                 schooling=c('read_write', 'high school', 'elementary', 'elementary', 'illiterate', 'college', 'elementary incomplete','high school incomplete','illiterate', 'elementary'),
                 schooling_code=c(2, 4, 3, 3, 1, 5, 3.5, 4.5, 1, 2),
                 num_voters=c(1, 4, 2, 3, 9, 10, 1, 8, 11, 3))

df_desired <- data.frame(Municipality=c('a', 'a','b'),
                 section=c(1,2,1),
                 state=c('ac', 'ac','mg'),
                 tot_elect=c(27, 9, 14),
                 tot_male=c(8, 1, 0),
                 share_male=c(0.29,0.11,0),
                 share_illiterate=c(0.33, 0, 0.78),
                 share_60_64=c(0.03, 0, 0.78))

DF_DESIED是我要获得的。感谢您的帮助。

到目前为止,我用过

aggregate(df$num_voters, by=list(df$Municipality,df$section,df$state), FUN=sum) 

Hi I am trying to do a pivot sum by group in R. Below my dataframe (my_df)

my_df <- data.frame(Municipality=c('a', 'a', 'a', 'a', 'a', 'a', 'a','a','b','b'),
                 section=c(1, 1, 1, 1, 1, 1, 2,2,1,1),
                 state=c('ac', 'ac', 'ac', 'ac', 'ac', 'ac', 'ac','ac','mg','mg'),
                 gender=c('male', 'male', 'female', 'male', 'female', 'female', 
                          'male','female','female','female'),
                 age=c('60-64', '17', '18', '18', '21-24', '30-34', '19','40-44','60-64','50-54'),
                 age_code=c('6064', '1700', '1800', '1800', '2124', '3034', 
                            '1900','4044','6064','5054'),
                 schooling=c('read_write', 'high school', 'elementary', 'elementary', 'illiterate', 'college', 'elementary incomplete','high school incomplete','illiterate', 'elementary'),
                 schooling_code=c(2, 4, 3, 3, 1, 5, 3.5, 4.5, 1, 2),
                 num_voters=c(1, 4, 2, 3, 9, 10, 1, 8, 11, 3))

df_desired <- data.frame(Municipality=c('a', 'a','b'),
                 section=c(1,2,1),
                 state=c('ac', 'ac','mg'),
                 tot_elect=c(27, 9, 14),
                 tot_male=c(8, 1, 0),
                 share_male=c(0.29,0.11,0),
                 share_illiterate=c(0.33, 0, 0.78),
                 share_60_64=c(0.03, 0, 0.78))

The df_desired is what I am trying to get. I would appreciate your help.

So far, I used

aggregate(df$num_voters, by=list(df$Municipality,df$section,df$state), FUN=sum) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖微凉心微凉 2025-01-28 22:08:54
library(dplyr)
my_df %>%
  group_by(Municipality, section, state) %>%
  summarize(tot_elect = sum(num_voters),
            tot_male = sum(num_voters[gender == "male"]),
            share_male = tot_male / tot_elect,
            share_illit = sum(num_voters[schooling == "illiterate"] / tot_elect),
            share_60_64 = sum(num_voters[age == "60-64"] / tot_elect))
library(dplyr)
my_df %>%
  group_by(Municipality, section, state) %>%
  summarize(tot_elect = sum(num_voters),
            tot_male = sum(num_voters[gender == "male"]),
            share_male = tot_male / tot_elect,
            share_illit = sum(num_voters[schooling == "illiterate"] / tot_elect),
            share_60_64 = sum(num_voters[age == "60-64"] / tot_elect))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文