簡體   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

第一個問題在這里。 Stackoverflow 一直是一個了不起的資源,所以感謝所有積極貢獻的人。

我需要在列中創建一個新變量。 該變量需要是其他三個變量的值的總和(在同一列中)。 這里的問題是,我還有一個兩年的專欄,需要保留這些年。 這里的問題是新變量將在兩個不同的地方重復,這需要如此。 (移植到 Tableau 以實現失業可視化)

我已經使用 Dplyr 進行了總結,希望能讓您了解我面臨的問題,以及如何最好地解決它。 鏈接到 Tibble

我需要將“Dun Laoghaire Rathdown”、“Dublin City”和“South Dublin”組合成一個名為“Dublin”的變量。 這些出現在 2011 年和 2016 年。需要刪除組合的三個變量,以便新變量“都柏林”出現在每個人口普查年的一行中(即 - 每個人口普查年刪除三個變量,一個變量稱為'都柏林'代替他們)

控制台 output 在我的 tibble 上使用 Dput:

結構(列表(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) 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.框架”))

提前致謝,

問候, R

像這樣:



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)

你沒有這么說,但我想你是想把這三個區的總數加起來,成為都柏林的總數?

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