Thanks, @GusMono, and I'm happy to give you suggestions on how to post questions here. From your ideal output, @nirgrahamuk's observation points to the issue -- the function cumsum()
returns a vector of the same length as the input:
a <- 1:4
a
#> [1] 1 2 3 4
cumsum(a)
#> [1] 1 3 6 10
where what you want is a single number (the last one produced by cumsum()
:
a
#> [1] 1 2 3 4
sum(a)
#> [1] 10
To share the 'Source #1' table, if its name is, say, source1
, then you would run the command
dput(source1)
and then copy and paste the output (from the console) into your post, but place it between a pair of triple backticks (```), like this:
```
# <-- place output of dput(source1) here
```
That allows others to copy and recreate your data on their own machines so they can help more easily. In your case, this is what you should get:
structure(list(fecha = structure(c(18346, 18346, 18397, 18397,
18397, 18430, 18430, 18430, 18431, 18432, 18433, 18434, 18434
), class = "Date"), carga_provincia_nombre = c("Chaco", "Chaco",
"Chaco", "Chaco", "Chaco", "Río Negro", "Río Negro", "Río Negro",
"Río Negro", "Río Negro", "Río Negro", "Río Negro", "Río Negro"
), Total_deaths = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -13L), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_double",
"collector")), fecha = structure(list(format = ""), class = c("collector_date",
"collector")), carga_provincia_nombre = structure(list(), class = c("collector_character",
"collector")), Total_deaths = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
If you copy and paste this into your own machine and execute, you'll see it recreates source1
.
In full, you'd want your post to look like this, with all the commands you need included:
library(tidyverse)
source1 <-
structure(list(fecha = structure(c(18346, 18346, 18397, 18397,
18397, 18430, 18430, 18430, 18431, 18432, 18433, 18434, 18434
), class = "Date"), carga_provincia_nombre = c("Chaco", "Chaco",
"Chaco", "Chaco", "Chaco", "Río Negro", "Río Negro", "Río Negro",
"Río Negro", "Río Negro", "Río Negro", "Río Negro", "Río Negro"
), Total_deaths = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -13L), spec = structure(list(
cols = list(id = structure(list(), class = c("collector_double",
"collector")), fecha = structure(list(format = ""), class = c("collector_date",
"collector")), carga_provincia_nombre = structure(list(), class = c("collector_character",
"collector")), Total_deaths = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
source1
#> # A tibble: 13 x 3
#> fecha carga_provincia_nombre Total_deaths
#> <date> <chr> <dbl>
#> 1 2020-03-25 Chaco 1
#> 2 2020-03-25 Chaco 1
#> 3 2020-05-15 Chaco 1
#> 4 2020-05-15 Chaco 1
#> 5 2020-05-15 Chaco 1
#> 6 2020-06-17 Río Negro 1
#> 7 2020-06-17 Río Negro 1
#> 8 2020-06-17 Río Negro 1
#> 9 2020-06-18 Río Negro 1
#> 10 2020-06-19 Río Negro 1
#> 11 2020-06-20 Río Negro 1
#> 12 2020-06-21 Río Negro 1
#> 13 2020-06-21 Río Negro 1
source1 %>%
group_by(fecha, carga_provincia_nombre) %>%
summarise(cum_cases = sum(Total_deaths))
#> # A tibble: 7 x 3
#> # Groups: fecha [7]
#> fecha carga_provincia_nombre cum_cases
#> <date> <chr> <dbl>
#> 1 2020-03-25 Chaco 2
#> 2 2020-05-15 Chaco 3
#> 3 2020-06-17 Río Negro 3
#> 4 2020-06-18 Río Negro 1
#> 5 2020-06-19 Río Negro 1
#> 6 2020-06-20 Río Negro 1
#> 7 2020-06-21 Río Negro 2
Created on 2020-07-05 by the reprex package (v0.3.0)
I hope this helps!