通过索引选择性地组合R数据帧

时间:2018-08-07 21:41:14

标签: r sum aggregate

我需要找到一种通过索引选择性地对行求和的方法。这是一个r数据集示例:

var plugin = function(){
  return function(style){
    style.define('color', function(color) {
      return color
    })
  }
}
module.exports = plugin

我希望得到:

Year  Category  Value  Index 
2002   A         100     1 
2002   B         200     1
2002   C         300     1
2002   A         400     2
2002   B         700     2
2002   C         900     2
2002   A         600     1  
2002   B         500     1
2002   C         400     1

2003   A         100     3 
2003   B         200     3
2003   C         300     3
2003   A         400     2
2003   B         700     2
2003   C         900     2
2003   A         600     2  
2003   B         500     2
2003   C         400     2

基本上,它们只会根据不同年份的类别汇总相同的索引。有没有一种有效的方法可以做到这一点而不是四处循环?我在这里有很长的索引列表,所以我认为循环是个坏主意。

2 个答案:

答案 0 :(得分:2)

您可以使用group_bysummarise。我不确定您给定的期望输出是否完全正确,除非我误解了这个问题。

library(tidyverse)
tbl <- structure(list(Year = c(2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L), Category = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C"), Value = c(100L, 200L, 300L, 400L, 700L, 900L, 600L, 500L, 400L, 100L, 200L, 300L, 400L, 700L, 900L, 600L, 500L, 400L), Index = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, -18L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = list(Year = structure(list(), class = c("collector_integer", "collector")), Category = structure(list(), class = c("collector_character", "collector")), Value = structure(list(), class = c("collector_integer", "collector")), Index = structure(list(), class = c("collector_integer", "collector"))), default = structure(list(), class = c("collector_guess", "collector"))), class = "col_spec"))

tbl %>%
  group_by(Year, Category, Index) %>%
  summarise(Value = sum(Value)) %>%
  arrange(Year, Index, Category)
#> # A tibble: 12 x 4
#> # Groups:   Year, Category [6]
#>     Year Category Index Value
#>    <int> <chr>    <int> <int>
#>  1  2002 A            1   700
#>  2  2002 B            1   700
#>  3  2002 C            1   700
#>  4  2002 A            2   400
#>  5  2002 B            2   700
#>  6  2002 C            2   900
#>  7  2003 A            2  1000
#>  8  2003 B            2  1200
#>  9  2003 C            2  1300
#> 10  2003 A            3   100
#> 11  2003 B            3   200
#> 12  2003 C            3   300

reprex package(v0.2.0)于2018-08-07创建。

答案 1 :(得分:2)

基本R:

aggregate(a$Value, a[c("Year", "Category", "Index")], sum)
#    Year Category Index    x
# 1  2002        A     1  700
# 2  2002        B     1  700
# 3  2002        C     1  700
# 4  2002        A     2  400
# 5  2003        A     2 1000
# 6  2002        B     2  700
# 7  2003        B     2 1200
# 8  2002        C     2  900
# 9  2003        C     2 1300
# 10 2003        A     3  100
# 11 2003        B     3  200
# 12 2003        C     3  300

我特别喜欢公式界面,我觉得它更具可读性:

aggregate(Value ~ Year + Category + Index, a, sum)
aggregate(. ~ Year + Category + Index, a, sum) # multiple columns to be summed

(全部显示在@CalumYou提供的链接中。)


数据:

a <- read.table(header=TRUE, text='
Year  Category  Value  Index 
2002   A         100     1 
2002   B         200     1
2002   C         300     1
2002   A         400     2
2002   B         700     2
2002   C         900     2
2002   A         600     1  
2002   B         500     1
2002   C         400     1
2003   A         100     3 
2003   B         200     3
2003   C         300     3
2003   A         400     2
2003   B         700     2
2003   C         900     2
2003   A         600     2  
2003   B         500     2
2003   C         400     2')