【问题标题】:How to take the average for several columns while rolling up categorical column in R?在R中滚动分类列时如何取几列的平均值?
【发布时间】:2019-10-05 01:03:47
【问题描述】:

我有一个数据框包含前 20 部电影信息,但没有电影名称,我想通过取 4 列的平均值来汇总数据框:runtime_minimdb_ratingvotesgross_millions同时汇总其他分类列genre1year。我不确定如何同时做到这一点。

这是数据框前 ​​40 行的dput

top_20_movies <- structure(list(genre1 = c("Adventure", "Animation", "Comedy", 
"Comedy", "Comedy", "Drama", "Drama", "Adventure", "Drama", "Action", 
"Drama", "Crime", "Drama", "Comedy", "Adventure", "Adventure", 
"Biography", "Action", "Western", "Action", "Adventure", "Horror", 
"Adventure", "Comedy", "Action", "Comedy", "Adventure", "Drama", 
"Comedy", "Comedy", "Drama", "Drama", "Comedy", "Comedy", "Drama", 
"Drama", "Comedy", "Comedy", "Action", "Comedy"), runtime_min = c(212, 
75, 121, 124, 102, 125, 114, 136, 149, 141, 141, 160, 134, 120, 
132, 105, 180, 97, 122, 88, 126, 109, 197, 174, 208, 125, 162, 
109, 127, 91, 149, 146, 112, 180, 103, 126, 119, 104, 128, 91
), imdb_rating = c(8.1, 7.3, 8.2, 7.3, 7.5, 7.9, 7.6, 8.3, 7.6, 
8, 6.2, 8, 7.2, 6.3, 7.1, 4.4, 7.4, 7.1, 7.2, 6.6, 7.2, 8.5, 
7.9, 8.1, 6.8, 8.3, 6.9, 6.4, 6.6, 6, 6.7, 7.8, 6.5, 5.5, 6.8, 
7.1, 6.5, 6.6, 7.8, 6), votes = c(200456, 121718, 218547, 11149, 
13698, 13254, 12511, 269682, 8455, 50602, 2608, 53688, 10867, 
2123, 13885, 1747, 10978, 3862, 4740, 1023, 12168, 533018, 112656, 
58658, 8105, 142271, 13056, 5117, 18193, 731, 2246, 9227, 3598, 
746, 4001, 1947, 6009, 4055, 80449, 2711), gross_millions = c(74.7, 
51.6, 25, 23.3, 18.75, 13.99, 13.9, 13.28, 12.8, 12.54, 12.2, 
11.9, 11, 11, 10, 5.45, 5.01, 4.02, 3.71, 2.18, 40.36, 32, 30, 
19.52, 18.97, 18.6, 17.26, 16.46, 12.32, 12, 11.34, 11.34, 11.1, 
9.6, 7.4, 7.3, 6.54, 6, 4.91, 4.7), year = c(1959, 1959, 1959, 
1959, 1959, 1959, 1959, 1959, 1959, 1959, 1959, 1959, 1959, 1959, 
1959, 1959, 1959, 1959, 1959, 1959, 1960, 1960, 1960, 1960, 1960, 
1960, 1960, 1960, 1960, 1960, 1960, 1960, 1960, 1960, 1960, 1960, 
1960, 1960, 1960, 1960)), row.names = c(51L, 52L, 53L, 54L, 55L, 
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 
69L, 70L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L
), class = "data.frame")

并且我想达到这种结果(请注意,结果中的所有平均数字现在都是虚构的数字):

genre1    runtime_min  imdb_rating    votes     gross_millions    year
Action      102          8.1         123585        88.6           1959
Adventure    98          7.5          56868        56.2           1959
.....
Action       120          8.8         113685        101.5         1960
Adventure    108          6.3          25868        68.9          1960

谢谢。

【问题讨论】:

    标签: r aggregate mean


    【解决方案1】:

    我们可以按genre1year 分组,然后总结所有变量的平均值

    tidyverse 方法:

    > library(tidyverse)
    > top_20_movies %>%
        group_by(genre1, year) %>% 
        summarise_all(mean)
    # A tibble: 13 x 6
    # Groups:   genre1 [?]
       genre1     year runtime_min imdb_rating   votes gross_millions
       <chr>     <dbl>       <dbl>       <dbl>   <dbl>          <dbl>
     1 Action     1959        109.        7.23  18496.           6.25
     2 Action     1960        168         7.3   44277           11.9 
     3 Adventure  1959        146.        6.98 121442.          25.9 
     4 Adventure  1960        162.        7.33  45960           29.2 
     5 Animation  1959         75         7.3  121718           51.6 
     6 Biography  1959        180         7.4   10978            5.01
     7 Comedy     1959        117.        7.32  61379.          19.5 
     8 Comedy     1960        125.        6.68  26330.          11.2 
     9 Crime      1959        160         8     53688           11.9 
    10 Drama      1959        133.        7.3    9539           12.8 
    11 Drama      1960        127.        6.96   4508.          10.8 
    12 Horror     1960        109         8.5  533018           32   
    13 Western    1959        122         7.2    4740            3.71
    

    基于 R 的方法

    aggregate(.~genre1+year, data=top_20_movies, FUN=mean)
    

    【讨论】:

    • 这太棒了!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2019-03-08
    • 1970-01-01
    • 2021-08-18
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多