简体   繁体   English

如何基于组合同一列中多个变量的值创建新变量并删除组合时使用的旧变量

[英]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. Stackoverflow 一直是一个了不起的资源,所以感谢所有积极贡献的人。

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) (移植到 Tableau 以实现失业可视化)

I have summarized using Dplyr to hopefully give you an an idea of the issue I'm facing, and how best to resolve it.我已经使用 Dplyr 进行了总结,希望能让您了解我面临的问题,以及如何最好地解决它。 Link to Tibble链接到 Tibble

I need to combine 'Dun Laoghaire Rathdown', 'Dublin City' and 'South Dublin' into one variable called 'Dublin'.我需要将“Dun Laoghaire Rathdown”、“Dublin City”和“South Dublin”组合成一个名为“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)这些出现在 2011 年和 2016 年。需要删除组合的三个变量,以便新变量“都柏林”出现在每个人口普查年的一行中(即 - 每个人口普查年删除三个变量,一个变量称为'都柏林'代替他们)

Console output using Dput on my tibble:控制台 output 在我的 tibble 上使用 Dput:

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",结构(列表(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,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, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L), County.and.City = c("Carlow",克莱尔”、“科克市”、“科克县”、“Dún Laoghaire-Rathdown”、“多尼戈尔”、“都柏林市”、“芬格尔”、“戈尔韦市”、“戈尔韦县”、“克里”、“基尔代尔” 、“基尔肯尼”、“老挝”、“利特里姆”、“利默里克市和县”、“朗福德”、“劳斯”、“梅奥”、“米斯”、“莫纳汉”、“奥法利”、“罗斯康芒”、“斯莱戈” ”、“南都柏林”、“蒂珀雷里”、“沃特福德市和县”、“韦斯特米斯”、“韦克斯福德”、“威克洛”、“卡洛”、“卡文”、“克莱尔”、“科克市”、“科克县” ", "Dún Laoghaire-Rathdown", "多尼戈尔", "都柏林市", "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 “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) Census_Year = c(2011L, 2016L), .rows = structure(list(1:31, 32:62), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))) , 行名称es = 1:2, class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame")) es = 1:2, class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.框架”))

Thanks in advance,提前致谢,

Regards, R问候, 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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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