我想在一列中总结数字,只要它们与它们具有相同的日期。问题是日期不会定期更改。 例如,我的数据集如下所示:
Date Amount
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/15/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/19/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
12/06/2016 2300
实际金额各不相同。输出应该是一列中的总计金额和另一列中的相应日期。我考虑过使用group_by或for循环,但是我对这两个问题都很困难,因为我对R很新。发现了一些类似的问题,但找不到任何回答我的问题。 非常感谢任何帮助。
答案 0 :(得分:1)
library(tidyverse)
df <- structure(list(Date = c("12/15/2016", "12/15/2016", "12/15/2016", "12/15/2016", "12/15/2016", "12/15/2016", "12/15/2016", "12/15/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/19/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016", "12/06/2016"), Amount = c(2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L, 2300L)), row.names = c(NA, -31L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(cols = list(Date = structure(list(), class = c("collector_character", "collector")), Amount = structure(list(), class = c("collector_integer", "collector"))), default = structure(list(), class = c("collector_guess", "collector"))), class = "col_spec"))
df %>%
group_by(Date) %>%
summarise(amount_sum = sum(Amount))
#> # A tibble: 3 x 2
#> Date amount_sum
#> <chr> <int>
#> 1 12/06/2016 27600
#> 2 12/15/2016 18400
#> 3 12/19/2016 25300
由reprex package(v0.2.0)创建于2018-06-05。