Hello @Miguelito ,
The example data you provided is useful.
The table function that you used before is a summary function : summing 1 for each observation.
To sum spending you can use the functionality of the dplyr package.
See the example below where:
- df2 contains the summary in 'long form' and contains the information you want
- df3 contains the same information in 'wide form' that resembles the
table output.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
df1 <-
structure(list(pais = c(10L, 15L, 15L, 10L, 15L, 15L, 15L, 15L,
10L, 15L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 15L), ccaa = c(16L, 13L, 13L, 13L, 13L, 7L, 9L, 12L,
17L, 9L, 13L, 9L, 9L, 7L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 9L), gastototal = c(2104.8327842, 3339.4242179, 2527.0036036,
1076.4172965, 2812.0859804, 6686.339234, 2196.4232084, 6941.6912321,
2085.3796211, 3355.3558589, 3422.6737527, 2697.8463611, 1750.2838905,
4599.6563689, 1444.3916741, 1403.0404296, 1345.220257, 1303.2814619,
704.5863107, 1345.4311241, 1495.1874334, 1074.8804203, 1345.4311241,
1252.3900093, 997.72492846)), row.names = c(NA, 25L), class = "data.frame")
df2 <- df1 %>%
group_by(pais,ccaa) %>%
summarise(gastototal=sum(gastototal),.groups = "drop") %>%
arrange(ccaa,pais)
df3 <- df2 %>%
pivot_wider(id_cols=pais,names_from = ccaa,
names_prefix="ccaa",values_from = gastototal,
values_fill=0) %>%
print()
#> # A tibble: 3 x 8
#> pais ccaa4 ccaa7 ccaa9 ccaa12 ccaa13 ccaa16 ccaa17
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 6 8521. 0 0 0 0 0 0
#> 2 15 0 11286. 10998. 6942. 12101. 0 0
#> 3 10 0 0 4193. 0 1076. 2105. 2085.
Created on 2021-11-29 by the reprex package (v2.0.0)