简体   繁体   中英

How Can I create a new variable, based on combining the values of several variables in same column and remove the old variables used when combining

First ever question on here. Stackoverflow has been an amazing resource so thank you to all who actively contribute.

I need to create a new variable in a column. This variable needs to be the sum of the values of three other variables (in the same column). The problem here, is that I have another column with two years, and need to keep the years. Issue here being the new variable would be duplicated in two different places and this needs to be the case. (Porting to Tableau for visualization of unemployment)

I have summarized using Dplyr to hopefully give you an an idea of the issue I'm facing, and how best to resolve it. Link to Tibble

I need to combine 'Dun Laoghaire Rathdown', 'Dublin City' and 'South Dublin' into one variable called 'Dublin'. These appear in both 2011, and 2016. The three variables being combined need to be removed so that the new variable 'Dublin' appears in their place as one row per census year (ie - three variables removed per census year, and one variable called 'Dublin' going in their place)

Console output using Dput on my tibble:

structure(list(Census_Year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L), County.and.City = c("Carlow", "Cavan", "Clare", "Cork City", "Cork County", "Dún Laoghaire-Rathdown", "Donegal", "Dublin City", "Fingal", "Galway City", "Galway County", "Kerry", "Kildare", "Kilkenny", "Laois", "Leitrim", "Limerick City and County", "Longford", "Louth", "Mayo", "Meath", "Monaghan", "Offaly", "Roscommon", "Sligo", "South Dublin", "Tipperary", "Waterford City and County", "Westmeath", "Wexford", "Wicklow", "Carlow", "Cavan", "Clare", "Cork City", "Cork County", "Dún Laoghaire-Rathdown", "Donegal", "Dublin City", "Fingal", "Galway City", "Galway County", "Kerry", "Kildare", "Kilkenny", "Laois", "Leitrim", "Limerick City and County", "Longford", "Louth", "Mayo", "Meath", "Monaghan", "Offaly", "Roscommon", "Sligo", "South Dublin", "Tipperary", "Waterford City and County", "Westmeath", "Wexford", "Wicklow"), Total = c(5908L, 7331L, 10542L, 12266L, 28603L, 11071L, 18869L, 51699L, 22640L, 7234L, 15311L, 13519L, 18639L, 8992L, 8260L, 3047L, 18537L, 4562L, 13792L, 11866L, 16292L, 5982L, 8313L, 5856L, 5624L, 26039L, 14996L, 11572L, 8637L, 16170L, 12674L, 4507L, 5413L, 7018L, 8943L, 18287L, 7716L, 12829L, 39200L, 15415L, 5175L, 9938L, 8701L, 12297L, 6044L, 6068L, 2163L, 12935L, 3701L, 10058L, 8591L, 10523L, 3831L, 5744L, 3847L, 4250L, 18265L, 10867L, 8323L, 6666L, 11478L, 8603L)), row.names = c(NA, -62L), groups = structure(list(Census_Year = c(2011L, 2016L), .rows = structure(list(1:31, 32:62), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.nam es = 1:2, class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

Thanks in advance,

Regards, R

Like so:



df <- structure(list(Census_Year = c(2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L), County.and.City = c("Carlow", "Cavan", "Clare", "Cork City", "Cork County", "Dún Laoghaire-Rathdown", "Donegal", "Dublin City", "Fingal", "Galway City", "Galway County", "Kerry", "Kildare", "Kilkenny", "Laois", "Leitrim", "Limerick City and County", "Longford", "Louth", "Mayo", "Meath", "Monaghan", "Offaly", "Roscommon", "Sligo", "South Dublin", "Tipperary", "Waterford City and County", "Westmeath", "Wexford", "Wicklow", "Carlow", "Cavan", "Clare", "Cork City", "Cork County", "Dún Laoghaire-Rathdown", "Donegal", "Dublin City", "Fingal", "Galway City", "Galway County", "Kerry", "Kildare", "Kilkenny", "Laois", "Leitrim", "Limerick City and County", "Longford", "Louth", "Mayo", "Meath", "Monaghan", "Offaly", "Roscommon", "Sligo", "South Dublin", "Tipperary", "Waterford City and County", "Westmeath", "Wexford", "Wicklow"), Total = c(5908L, 7331L, 10542L, 12266L, 28603L, 11071L, 18869L, 51699L, 22640L, 7234L, 15311L, 13519L, 18639L, 8992L, 8260L, 3047L, 18537L, 4562L, 13792L, 11866L, 16292L, 5982L, 8313L, 5856L, 5624L, 26039L, 14996L, 11572L, 8637L, 16170L, 12674L, 4507L, 5413L, 7018L, 8943L, 18287L, 7716L, 12829L, 39200L, 15415L, 5175L, 9938L, 8701L, 12297L, 6044L, 6068L, 2163L, 12935L, 3701L, 10058L, 8591L, 10523L, 3831L, 5744L, 3847L, 4250L, 18265L, 10867L, 8323L, 6666L, 11478L, 8603L)), row.names = c(NA, -62L), groups = structure(list(Census_Year = c(2011L, 2016L), .rows = structure(list(1:31, 32:62), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

i <- grepl( "Dublin|Dun Laoghaire Rathdown", df$County.and.City )
df$County.and.City[ i ] <- "Dublin"

df %<>% group_by( Census_Year, County.and.City ) %>%
    summarize( Total = sum(Total) )

print.data.frame(df)

You didn't say so, but I assume you meant to sum up the total for these three districts, to a Dublin total?

Output:


   Census_Year           County.and.City Total
1         2011                    Carlow  5908
2         2011                     Cavan  7331
3         2011                     Clare 10542
4         2011                 Cork City 12266
5         2011               Cork County 28603
6         2011   Dún Laoghaire-Rathdown 11071
7         2011                   Donegal 18869
8         2011                    Dublin 77738
9         2011                    Fingal 22640
10        2011               Galway City  7234
11        2011             Galway County 15311
12        2011                     Kerry 13519
13        2011                   Kildare 18639
14        2011                  Kilkenny  8992
15        2011                     Laois  8260
16        2011                   Leitrim  3047
17        2011  Limerick City and County 18537
18        2011                  Longford  4562
19        2011                     Louth 13792
20        2011                      Mayo 11866
21        2011                     Meath 16292
22        2011                  Monaghan  5982
23        2011                    Offaly  8313
24        2011                 Roscommon  5856
25        2011                     Sligo  5624
26        2011                 Tipperary 14996
27        2011 Waterford City and County 11572
28        2011                 Westmeath  8637
29        2011                   Wexford 16170
30        2011                   Wicklow 12674
31        2016                    Carlow  4507
32        2016                     Cavan  5413
33        2016                     Clare  7018
34        2016                 Cork City  8943
35        2016               Cork County 18287
36        2016   Dún Laoghaire-Rathdown  7716
37        2016                   Donegal 12829
38        2016                    Dublin 57465
39        2016                    Fingal 15415
40        2016               Galway City  5175
41        2016             Galway County  9938
42        2016                     Kerry  8701
43        2016                   Kildare 12297
44        2016                  Kilkenny  6044
45        2016                     Laois  6068
46        2016                   Leitrim  2163
47        2016  Limerick City and County 12935
48        2016                  Longford  3701
49        2016                     Louth 10058
50        2016                      Mayo  8591
51        2016                     Meath 10523
52        2016                  Monaghan  3831
53        2016                    Offaly  5744
54        2016                 Roscommon  3847
55        2016                     Sligo  4250
56        2016                 Tipperary 10867
57        2016 Waterford City and County  8323
58        2016                 Westmeath  6666
59        2016                   Wexford 11478
60        2016                   Wicklow  8603

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM