【问题标题】:How to make a frequency table by class [duplicate]如何按类别制作频率表[重复]
【发布时间】:2020-01-28 18:07:09
【问题描述】:

这是我目前设置的代码:

library(dslabs)
library(dplyr)
library(lubridate)

data("reported_heights")

dat <- mutate(reported_heights, date_time = ymd_hms(time_stamp)) %>%
  filter(date_time >= make_date(2016, 01, 25) & date_time < make_date(2016, 02, 1)) %>%
  mutate(type = ifelse(day(date_time) == 25 & hour(date_time) == 8 & between(minute(date_time), 15, 30), "inclass","online")) %>%
  select(sex, type, time_stamp)

y <- factor(dat$sex, c("Female", "Male"))
x <- dat$type

counter <- count(dat, sex,type)

它为我创建了一个看起来像这样的 tbl_df,链接如下:

      sex | type    | n 
1  Female | inclass | 26
2  Male   | inclass | 13
3  Female | online  | 42
4  Male   | online  | 69

我在问你是否可以帮助我编写一个代码来计算每种类型中每种性别的比例。

我一直在尝试创建一个新表,使用 x 字符“inclass”和“online”作为添加比例列的列,然后将 y 因子“男性”和“女性”作为行。我一直在尝试使用pull()prop.table() 来做到这一点,但我是一个完全的新手,如果你们美丽的专家可以帮助我,这对我来说意味着世界。我已经浏览了几个小时的答案,也许答案已经出来了,所以请原谅我似乎找不到它....非常感谢。

每类班级(inclass&online)的性别(男&女)比例是多少?

可以通过将性别除以给定类型班级的学生总数来计算。

例如:在总数 (42+69)=111 中,有 42 名女性在线学习。答:在线课程中 38% 是女性。

我们如何在 R 中做到这一点?

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用table()

    my.table <- with(dat, table(sex, type))
    my.table
    #         type
    # sex      inclass online
    #   Female      26     42
    #   Male        13     69
    

    apply()对结果的一个函数。

    res <- apply(my.table, 2, function(x) x/sum(x)*100) 
    res
    #         type
    # sex       inclass   online
    #   Female 66.66667 37.83784
    #   Male   33.33333 62.16216
    

    要获得更好的输出,您可以 round() 然后添加 %

    res2 <- as.data.frame(unclass(round(res, 1)))
    res2[] <- lapply(res2, paste0, "%")
    res2
    #        inclass online
    # Female   66.7%  37.8%
    # Male     33.3%  62.2%
    

    【讨论】:

      【解决方案2】:

      要得到每个类的比例,我们可以在base R中使用ave

      df$prop <- with(df, n/ave(n, type, FUN = sum)) * 100 
      df
      #     sex    type  n     prop
      #1 Female inclass 26 66.66667
      #2   Male inclass 13 33.33333
      #3 Female  online 42 37.83784
      #4   Male  online 69 62.16216
      

      dplyr也可以达到同样的效果

      library(dplyr)
      df %>% group_by(type) %>% mutate(prop = n/sum(n) * 100)
      

      data.table

      library(data.table)
      setDT(df)[, prop := n/sum(n) * 100, by = type]
      

      数据

      df <- structure(list(sex = structure(c(1L, 2L, 1L, 2L), .Label = c("Female", 
      "Male"), class = "factor"), type = structure(c(1L, 1L, 2L, 2L
      ), .Label = c("inclass", "online"), class = "factor"), n = c(26L, 
      13L, 42L, 69L)), class = "data.frame", row.names = c(NA, -4L))
      

      【讨论】:

        【解决方案3】:

        使用prop.table()

        prop.table(table(y, x), 2)
        #        x
        #y          inclass    online
        #  Female 0.6666667 0.3783784
        #  Male   0.3333333 0.6216216
        

        【讨论】:

        • 这是一种很好的方式,只需一行就可以完成我的目标。真棒@Cole 谢谢:D
        猜你喜欢
        • 2018-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-06
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 2019-05-12
        相关资源
        最近更新 更多