将列值汇总并聚合为 R 中的行

summarising and aggregating column values as rows in R

我的数据框主要包含分类列和一个数字列,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 来做到这一点,但没有取得多大成功,我们将不胜感激。

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

首先,我们gather(来自tidyr)将所有非租金列转换为长格式(运行 只是这两行以查看结果)。然后 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

(请注意,对于 "Silicon Valley",您的数据有 "V" 和 "v",导致两条单独的线。)

我们可以使用data.table。将'data.frame'转换为'data.table'(setDT(df1)),melt从'wide'转换为'long'格式,按'variable',[=分组25=](从 melt 创建的列),我们创建两列 'No_of_occur'、'SUM_RENT' 作为行数 (.N) 和 sum 'Rent'列,然后按'variable'、'No_of_occur'和'SUM_RENT'分组,得到'value'列的unique个元素('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