【问题标题】:Frequency count in RR中的频率计数
【发布时间】:2020-06-30 04:17:58
【问题描述】:

我希望它显示国内总频率(包括波士顿+盐湖城)和国际总频率(伦敦+上海)的频率表。但它会像这样打印出来。

table$Category<-c("Domestic","International")
> table
  problem.6.data Freq      Category
1         Boston  136      Domestic
2         London  102 International
3 Salt Lake City  277      Domestic
4       Shanghai  184 International


I want an output of: 

 1. Domestic: 136+277
 2. International: 102+ 184

所以,最后表格应该是这样的:

  1. 国内:413
  2. 国际:286

我做错了什么?

【问题讨论】:

  • 嗨,试试table(table$Category)希望这会有所帮助。

标签: r frequency-table


【解决方案1】:

如果你不介意使用 tidyverse,可以使用 group_by() 和 summarize():

library(tidyverse)

df <-
    data.frame(
        stringsAsFactors = FALSE,
        problem.6.data = c("Boston", "London", "Salt Lake City", "Shanghai"),
        Freq = c(136L, 102L, 277L, 184L),
        Category = c("Domestic", "International", "Domestic", "International")
    )

df %>% 
    group_by(Category) %>% 
    summarise(sum = sum(Freq))
#> # A tibble: 2 x 2
#>   Category        sum
#>   <chr>         <int>
#> 1 Domestic        413
#> 2 International   286

由reprex package (v0.3.0) 于 2020-03-19 创建

【讨论】:

    【解决方案2】:

    也许来自基础 R 的 aggregate 可以提供所需的输出

    dfout <- aggregate(Freq ~ Category, df, sum)
    

    这样

    > dfout
           Category Freq
    1      Domestic  413
    2 International  286
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多