R语言 如何获得一年内每个国家不同观测值随时间变化的平均值?非数字问题

col17t5w  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

我正在分析一段时间以来的议会数据,关于某一年妇女在议会中的百分比。有些年,由于妇女在议会中的百分比在这一年中经历了发展,因此有多种观察结果。因此,我想写一个代码,给我:
一段时间内某一年女性任职人数的平均数,然后按国家分组。
我的问题是,R不断返回NA值和一个警告,说我有非数值数据在我的数据集。然而,我似乎找不到这个非数值数据。我只有数字在百分比列。它可能是.在0.5给我的麻烦?
我尝试了以下代码在dqr:

Newlevelgenparla <- genparla %>%
  group_by(Country, Year) %>%
  summarize(Percent_women_in_parliament = mean(`Percent Of Women in Chamber`, na.rm = TRUE))

字符串

Newlevelgenparla <- genparla %>%
  group_by(Year) %>%
  summarize(Percent_women_in_parliament = mean(`Percent Of Women in Chamber`, na.rm = TRUE))


然而,后者也给了我NA,并没有按国家分组。所以这不是我要找的。
如果您能给我任何给予帮助,我将不胜感激!
编辑:dput(head(genparla))的输出:

structure(list(Country = c("Afghanistan", "Afghanistan", "Afghanistan", 
"Afghanistan", "Afghanistan", "Afghanistan"), Region = c("ASIA", 
"ASIA", "ASIA", "ASIA", "ASIA", "ASIA"), `Election / Renewal` = c("Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes"), Year = c(1965, 1965, 1969, 
1969, 1988, 1988), Month = c("July", "July", NA, NA, NA, NA), 
    `Chamber Type` = c("Lower", "Upper", "Lower", "Upper", "Lower", 
    "Upper"), `Chamber Total Seats` = c("210", "84", "216", "84", 
    "235", "173"), `Total women` = c("4", "0", "0", "0", "7", 
    "0"), `Percent Of Women in Chamber` = c("0.019047619047619", 
    "0", "0", "0", "0.0297872340425532", "0"), NOTES = c("First legislature of Afghanistan after the abolition of the monarchy in 1964", 
    "First legislature of Afghanistan after the abolition of the monarchy in 1964", 
    "Parliament was dissolved following a military coup d'Etat in July 1973.", 
    "Parliament was dissolved following a military coup d'Etat in July 1973.", 
    "Collapse of constitutional institutions, including Parliament, in April 1992.  A Council of Decision and Settlement or Constituent Assembly, comprising 1335 members met in December 1992 and January 1993 and elected from among its ranks a 250-member interim Assembly. Pending general elections: no available information concerning the number of women members of the Council or the interim Assembly.", 
    "Collapse of constitutional institutions, including Parliament, in April 1992.  A Council of Decision and Settlement or Constituent Assembly, comprising 1335 members met in December 1992 and January 1993 and elected from among its ranks a 250-member interim Assembly. Pending general elections: no available information concerning the number of women members of the Council or the interim Assembly."
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

jc3wubiy

jc3wubiy1#

基于你的dput,我能够复制这个问题,这是一个可以帮助你解决这个问题的解决方案.你需要首先将商会总席位,总妇女和商会中妇女的百分比转换为数字,因为它们是字符类型,然后执行你的摘要.你可以这样做与dapur:

genparla <- genparla %>%
  mutate(
    `Chamber Total Seats` = as.numeric(`Chamber Total Seats`),
    `Total women` = as.numeric(`Total women`),
    `Percent Of Women in Chamber` = as.numeric(`Percent Of Women in Chamber`)
  )

字符串
然后,您可以执行汇总操作,现在两者都应该工作:

Newlevelgenparla1 <- genparla %>%
  group_by(Country, Year) %>%
  summarize(Percent_women_in_parliament = mean(`Percent Of Women in Chamber`, na.rm = TRUE))

Newlevelgenparla1

Newlevelgenparla2 <- genparla %>%
  group_by(Year) %>%
  summarize(Percent_women_in_parliament = mean(`Percent Of Women in Chamber`, na.rm = TRUE))

> Newlevelgenparla1
# A tibble: 3 × 3
# Groups:   Country [1]
  Country      Year Percent_women_in_parliament
  <chr>       <dbl>                       <dbl>
1 Afghanistan  1965                     0.00952
2 Afghanistan  1969                     0      
3 Afghanistan  1988                     0.0149 

> Newlevelgenparla2
# A tibble: 3 × 2
   Year Percent_women_in_parliament
  <dbl>                       <dbl>
1  1965                     0.00952
2  1969                     0      
3  1988                     0.0149

相关问题