【问题标题】:Refer to a list of variables within group calculation请参阅组计算中的变量列表
【发布时间】:2017-04-13 19:17:05
【问题描述】:

我想计算一个组中每个变量(总共 20 个)的最大值,我想知道有没有更简单的方法来执行计算,而不是使用 summarisegroup_bydplyr?示例数据如下:

Name    Year    test1   test2   test3   test4   test5   test6   test7   test8   test9   test10  test11  test12  test13  test14  test15  test16  test17  test18  test19  test20
John    2008    1   0   0   0   0   1   0   0   0   0   0   1   0   0   1   0   0   1   0   0
John    2008    1   0   1   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   1   0
John    2009    0   1   1   0   0   0   1   0   1   0   0   1   0   0   0   1   0   0   0   0
John    2010    0   0   0   1   0   1   1   0   0   0   1   0   1   0   0   0   1   0   0   1
John    2010    0   0   0   0   0   0   0   1   1   0   0   0   0   1   0   0   1   0   1   1
John    2010    0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0
John    2011    0   0   0   1   1   0   1   0   0   1   0   0   0   0   0   0   0   0   0   0
John    2011    0   0   0   0   1   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0
John    2012    0   0   0   1   0   0   1   0   1   0   0   1   0   0   0   0   0   0   0   0
John    2012    0   0   0   0   0   0   0   1   0   0   1   0   0   1   0   0   0   0   0   0
John    2012    0   0   1   0   0   0   1   0   0   0   0   0   0   0   0   1   1   0   0   1
John    2013    0   0   1   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0
Mary    2009    0   0   1   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Mary    2010    0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
Mary    2010    0   0   0   0   1   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
Mary    2011    0   1   0   0   0   0   0   0   1   0   0   0   0   0   0   0   1   0   0   1
Mary    2011    0   0   0   0   0   0   0   1   0   0   1   0   0   0   0   1   0   0   0   0
Mary    2011    0   0   1   1   0   0   1   0   0   0   0   0   1   0   1   0   0   0   0   0
Mary    2011    0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
Mary    2012    0   0   0   0   0   1   0   1   0   0   1   0   1   0   0   0   0   0   0   0
Mary    2012    0   0   0   0   1   0   0   0   0   1   0   1   0   0   0   0   0   0   0   0
Mary    2013    0   0   0   1   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0
Mary    2013    0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
Jack    2010    0   0   0   0   1   0   0   0   0   0   1   0   0   1   1   0   0   0   0   0
Jack    2010    0   0   0   0   0   0   0   0   0   1   0   0   0   0   1   0   0   0   0   0
Jack    2011    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0
Jack    2011    0   0   0   0   1   0   0   0   1   0   0   0   0   1   0   0   0   0   0   0
Jack    2011    0   0   0   0   0   1   0   0   0   1   0   0   0   0   0   0   0   0   0   0
Jack    2011    0   1   0   0   0   0   0   0   0   0   1   0   0   1   0   0   0   0   0   0
Jack    2012    0   0   1   1   0   0   0   0   1   1   0   0   1   0   0   0   0   0   0   0
Jack    2012    0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0
Jack    2013    1   0   0   0   0   1   0   0   0   0   1   0   0   0   0   0   0   0   0   0
Jack    2013    0   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0
Jack    2014    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
Jack    2015    0   0   0   1   0   1   1   0   0   0   1   0   1   0   0   0   1   0   0   1
Jack    2015    0   0   0   0   0   0   0   1   1   0   0   0   0   1   0   0   1   0   1   1
Jack    2015    0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0

test1test20 代表不同类型的测试,1 代表参加此考试的人,0 代表他/她不参加。一个人可以尽可能多地参加考试。我想要一个person-year 级别的聚合来证明这个人在那一年是否参加过每项考试。如上所述,是否有任何简单的方法可以计算所有 20 个测试的 person-year 级别中的 max?我正在考虑使用ddply,但仍在纠结是否有更好的方法。

提前致谢!
安妮

【问题讨论】:

    标签: r variables group-by dplyr plyr


    【解决方案1】:

    添加tidyr 会有所帮助:

    # highlighting your data above
    dat <- read.table("clipboard", header = TRUE, stringsAsFactors = FALSE)
    
    library(dplyr)
    library(tidyr)
    
    dat %>%
      gather(test, tookit, -Name, -Year) %>%
      group_by(Name, Year, test) %>%
      summarize(times = sum(tookit)) %>%
      ungroup()
    # # A tibble: 340 × 4
    #     Name  Year   test times
    #    <chr> <int>  <chr> <int>
    # 1   Jack  2010  test1     0
    # 2   Jack  2010 test10     1
    # 3   Jack  2010 test11     1
    # 4   Jack  2010 test12     0
    # 5   Jack  2010 test13     0
    # 6   Jack  2010 test14     1
    # 7   Jack  2010 test15     2
    # 8   Jack  2010 test16     0
    # 9   Jack  2010 test17     0
    # 10  Jack  2010 test18     0
    # # ... with 330 more rows
    

    这会告诉您他们每年参加每项考试的次数。

    另一种方法(不带tidyr):

    dat %>%
      group_by(Name, Year) %>%
      summarize_at(starts_with("test", vars=colnames(.)), sum) %>%
      ungroup()
    # A tibble: 17 × 22
    #     Name  Year test1 test2 test3 test4 test5 test6 test7 test8 test9 test10
    #    <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>  <int>
    # 1   Jack  2010     0     0     0     0     1     0     0     0     0      1
    # 2   Jack  2011     0     1     0     0     1     1     0     0     1      1
    # 3   Jack  2012     0     0     1     1     0     0     0     0     1      1
    # 4   Jack  2013     1     0     0     0     0     1     0     0     0      0
    # 5   Jack  2014     0     0     0     0     0     0     0     0     0      0
    # 6   Jack  2015     0     0     0     1     0     1     1     1     1      0
    # 7   John  2008     2     0     1     0     0     1     0     0     0      1
    # 8   John  2009     0     1     1     0     0     0     1     0     1      0
    # 9   John  2010     0     0     0     1     0     1     1     1     1      0
    # 10  John  2011     0     0     0     1     2     0     1     1     0      1
    # 11  John  2012     0     0     1     1     0     0     2     1     1      0
    # 12  John  2013     0     0     1     0     0     0     0     0     0      0
    # 13  Mary  2009     0     0     1     0     1     0     0     0     0      0
    # 14  Mary  2010     0     0     0     0     1     0     1     0     0      1
    # 15  Mary  2011     0     1     1     1     0     0     1     1     1      1
    # 16  Mary  2012     0     0     0     0     1     1     0     1     0      1
    # 17  Mary  2013     0     0     0     1     0     0     1     1     0      0
    # # ... with 10 more variables: test11 <int>, test12 <int>, test13 <int>,
    # #   test14 <int>, test15 <int>, test16 <int>, test17 <int>, test18 <int>,
    # #   test19 <int>, test20 <int>
    

    【讨论】:

    • 您好埃文,感谢您的快速帮助。我想保留面板,因为它只是获取每个变量的最大值,是否仍然可以使用这种方法?最好的,
    猜你喜欢
    • 2022-01-18
    • 2012-11-19
    • 1970-01-01
    • 2021-08-03
    • 2017-08-03
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多