【问题标题】:summarising and aggregating column values as rows in R将列值汇总为 R 中的行
【发布时间】:2016-11-04 20:44:26
【问题描述】:

我的数据框主要包含分类列和一个数字列,df 看起来像这样(简化):

**Home_type**     **Garden_type**       **NaighbourhoOd**    **Rent** 
Vila                big                  brooklyn             5000
Vila                small                bronx                7000
Condo               shared               Sillicon valley      2000 
Appartment          none                 brooklyn             500
Condo               none                 bronx                1700
Appartment          none                 Sillicon Valley      800 

对于每个分类列,我想显示其所有不同的值、频率和与之相关的租金总和。

结果应该是这样的:

**Variable**     **Distinct_values**      **No_of-Occurences**     **SUM_RENT**
  Home_type        Vila                     2                        12000
  Home_type        Condo                    2                        3700
  Home_type        Appartment               2                        1300
  Garden_type      big                      1                        5000
  Garden_type      small                    1                        7000
  Garden_type      shared                   1                        2000 
  Garden_type      none                     3                        3000 
  Naighbourhood    brooklyn                 2                        5500
  Naighbourhood    Bronx                    2                        8700 
  Naighbourhood    Sillicon Valley          2                        2800

我是 R 的新手,并尝试在 reshape2 中使用 melt 来做到这一点,但没有取得多大成功,任何帮助将不胜感激。

【问题讨论】:

  • 您可能需要查看this overview for asking good R questions,尤其是那些可以轻松读取数据的部分。如果我们不必费力地阅读您的数据,那么提供帮助会容易得多数据到 R.
  • 谢谢你给我指点马克,我以后一定会更加小心,稍后会编辑这篇文章。

标签: r statistics reshape2


【解决方案1】:

我最近更喜欢tidyr 而不是reshape2,尽管这主要是因为语法更类似于dplyr——由于加载了magrittr管道,这也将使这项任务变得更加容易(%>%) 和它的数据汇总工具。

首先,我们将gather(来自tidyr)所有非 Rent 列转换为长格式(仅运行这两行以查看结果)。然后group_by 要聚集在一起的列。最后,summarise 在每个组内获取您想要的指标。

df %>%
  gather(Variable, Distinct_Values, -Rent) %>%
  group_by(Variable, Distinct_Values) %>%
  summarise(
    `No_of-Occurences` = n()
    , SUM_RENT = sum(Rent)
  )

给予:

        Variable Distinct_Values `No_of-Occurences` SUM_RENT
           <chr>           <chr>              <int>    <int>
1    Garden_type             big                  1     5000
2    Garden_type            none                  3     3000
3    Garden_type          shared                  1     2000
4    Garden_type           small                  1     7000
5      Home_type      Appartment                  2     1300
6      Home_type           Condo                  2     3700
7      Home_type            Vila                  2    12000
8  NaighbourhoOd           bronx                  2     8700
9  NaighbourhoOd        brooklyn                  2     5500
10 NaighbourhoOd Sillicon valley                  1     2000
11 NaighbourhoOd Sillicon Valley                  1      800

(请注意,您的数据中有“V”和“v”表示“硅谷”,导致两条单独的行。)

【讨论】:

  • 效果很好,谢谢。我不知道 tidyr,肯定会更多地使用它。
【解决方案2】:

我们可以使用data.table。将“data.frame”转换为“data.table”(setDT(df1)),将melt从“宽”格式转换为“长”格式,按“变量”、“值”分组(从melt创建的列) ,我们创建两列'No_of_occur'、'SUM_RENT'作为'Rent'列的行数(.N)和sum,然后按'variable'、'No_of_occur'和'SUM_RENT'分组,得到unique 'value' 列的元素('Distinct_values')

library(data.table)
melt(setDT(df1), id.var=c('Rent'))[, c("No_of_occur", "SUM_RENT") :=
      .(.N, sum(Rent)) ,.(variable, value)][,
    .(Distinct_values = unique(value)) , .(variable, No_of_occur, SUM_RENT)]
 #         variable No_of_occur SUM_RENT Distinct_values
 #1:     Home_type           2    12000            Vila
 #2:     Home_type           2     3700           Condo
 #3:     Home_type           2     1300      Appartment
 #4:   Garden_type           1     5000             big
 #5:   Garden_type           1     7000           small
 #6:   Garden_type           1     2000          shared
 #7:   Garden_type           3     3000            none
 #8: NaighbourhoOd           2     5500        brooklyn
 #9: NaighbourhoOd           2     8700           bronx
 #10:NaighbourhoOd           2     2800 Sillicon Valley

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多