1

I would like to sum up numbers in one column as long as they have the same date associated with them. The problem is that the dates do not change on a regular basis. For example my data set would look like this:

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

The actual amounts are all different. The output should be the summed up amounts in one column and the corresponding dates in another. I thought about using group_by or a for loop but I got stuck with both of those since I am pretty new to R. Found a few similar questions but could not find any that answer my question. Any help is greatly appreciated.

fabi96
  • 25
  • 3
  • Hello and welcome. With packages dplyr/tidyverse you could certainly get your work done through groub_by(Date) and summarise(Amount = sum(ammount). Yet, it is expected that you point what you have tried and where did that go wrong. I would suggest you familiarize yourself with these guides: https://stackoverflow.com/help/how-to-ask – Nicolás Velasquez Jun 05 '18 at 18:37
  • `aggregate(data$Amount, list(data$Date), sum)` – G5W Jun 05 '18 at 18:40

1 Answers1

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

Created on 2018-06-05 by the reprex package (v0.2.0).

Calum You
  • 14,687
  • 4
  • 23
  • 42