首页 > 解决方案 > 如何压缩data.frame?

问题描述

在我开始之前:对不起问题的标题。我不知道如何用一句话来表达这个问题。

我正在处理一个大数据集,现在我想将世界银行的良好治理指标合并到我的主要 data.frame 中。来自世界银行的数据格式对于合并处理并不是很好。所以,我只想要几年的指标。

我使用了这段代码(我稍微缩短了代码):

gg_stack <- read.csv("WGIData.csv", sep= ",") 
dput(head(gg_stack, 10))

输入:

> dput(head(gg_stack, 10))
structure(list(Country.Name = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Afghanistan", "Albania", "Algeria", 
"American Samoa", "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", 
"Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 
"Bahamas, The", "Bahrain", "Bangladesh", "Barbados", "Belarus", 
"Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 
"Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", 
"Cameroon", "Canada", "Cayman Islands", "Central African Republic", 
"Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Dem. Rep.", 
"Congo, Rep.", "Cook Islands", "Costa Rica", "Cote d'Ivoire", 
"Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", 
"Dominica", "Dominican Republic", "Ecuador", "Egypt, Arab Rep.", 
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", 
"Fiji", "Finland", "France", "French Guiana", "Gabon", "Gambia, The", 
"Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", 
"Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", 
"Honduras", "Hong Kong SAR, China", "Hungary", "Iceland", "India", 
"Indonesia", "Iran, Islamic Rep.", "Iraq", "Ireland", "Israel", 
"Italy", "Jamaica", "Japan", "Jersey, Channel Islands", "Jordan", 
"Kazakhstan", "Kenya", "Kiribati", "Korea, Dem. People’s Rep.", 
"Korea, Rep.", "Kosovo", "Kuwait", "Kyrgyz Republic", "Lao PDR", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", 
"Lithuania", "Luxembourg", "Macao SAR, China", "Macedonia, FYR", 
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
"Marshall Islands", "Martinique", "Mauritania", "Mauritius", 
"Mexico", "Micronesia, Fed. Sts.", "Moldova", "Monaco", "Mongolia", 
"Montenegro", "Morocco", "Mozambique", "Myanmar", "Namibia", 
"Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "Niue", "Norway", "Oman", "Pakistan", 
"Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", 
"Russian Federation", "Rwanda", "Samoa", "San Marino", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovak Republic", "Slovenia", "Solomon Islands", 
"Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", 
"St. Kitts and Nevis", "St. Lucia", "St. Vincent and the Grenadines", 
"Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan, China", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", 
"Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", 
"Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", 
"United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", 
"Venezuela, RB", "Vietnam", "Virgin Islands (U.S.)", "West Bank and Gaza", 
"Yemen, Rep.", "Zambia", "Zimbabwe"), class = "factor"), Country.Code = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("ABW", "AFG", 
"AGO", "AIA", "ALB", "AND", "ANT", "ARE", "ARG", "ARM", "ASM", 
"ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", 
"BGR", "BHR", "BHS", "BIH", "BLR", "BLZ", "BMU", "BOL", "BRA", 
"BRB", "BRN", "BTN", "BWA", "CAF", "CAN", "CHE", "CHL", "CHN", 
"CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPV", "CRI", 
"CUB", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA", "DNK", "DOM", 
"DZA", "ECU", "EGY", "ERI", "ESP", "EST", "ETH", "FIN", "FJI", 
"FRA", "FSM", "GAB", "GBR", "GEO", "GHA", "GIN", "GMB", "GNB", 
"GNQ", "GRC", "GRD", "GRL", "GTM", "GUF", "GUM", "GUY", "HKG", 
"HND", "HRV", "HTI", "HUN", "IDN", "IND", "IRL", "IRN", "IRQ", 
"ISL", "ISR", "ITA", "JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", 
"KGZ", "KHM", "KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBR", 
"LBY", "LCA", "LIE", "LKA", "LSO", "LTU", "LUX", "LVA", "MAC", 
"MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", 
"MLT", "MMR", "MNE", "MNG", "MOZ", "MRT", "MTQ", "MUS", "MWI", 
"MYS", "NAM", "NER", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL", 
"NRU", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", "PLW", "PNG", 
"POL", "PRI", "PRK", "PRT", "PRY", "PSE", "QAT", "REU", "ROU", 
"RUS", "RWA", "SAU", "SDN", "SEN", "SGP", "SLB", "SLE", "SLV", 
"SMR", "SOM", "SRB", "SSD", "STP", "SUR", "SVK", "SVN", "SWE", 
"SWZ", "SYC", "SYR", "TCD", "TGO", "THA", "TJK", "TKM", "TLS", 
"TON", "TTO", "TUN", "TUR", "TUV", "TWN", "TZA", "UGA", "UKR", 
"URY", "USA", "UZB", "VCT", "VEN", "VIR", "VNM", "VUT", "WSM", 
"XKX", "YEM", "ZAF", "ZMB", "ZWE"), class = "factor"), Indicator.Name = structure(1:10, .Label = c("Control of Corruption: Estimate", 
"Control of Corruption: Number of Sources", "Control of Corruption: Percentile Rank", 
"Control of Corruption: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Control of Corruption: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Control of Corruption: Standard Error", "Government Effectiveness: Estimate", 
"Government Effectiveness: Number of Sources", "Government Effectiveness: Percentile Rank", 
"Government Effectiveness: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Government Effectiveness: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Government Effectiveness: Standard Error", "Political Stability and Absence of Violence/Terrorism: Estimate", 
"Political Stability and Absence of Violence/Terrorism: Number of Sources", 
"Political Stability and Absence of Violence/Terrorism: Percentile Rank", 
"Political Stability and Absence of Violence/Terrorism: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Political Stability and Absence of Violence/Terrorism: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Political Stability and Absence of Violence/Terrorism: Standard Error", 
"Regulatory Quality: Estimate", "Regulatory Quality: Number of Sources", 
"Regulatory Quality: Percentile Rank", "Regulatory Quality: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Regulatory Quality: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Regulatory Quality: Standard Error", "Rule of Law: Estimate", 
"Rule of Law: Number of Sources", "Rule of Law: Percentile Rank", 
"Rule of Law: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Rule of Law: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Rule of Law: Standard Error", "Voice and Accountability: Estimate", 
"Voice and Accountability: Number of Sources", "Voice and Accountability: Percentile Rank", 
"Voice and Accountability: Percentile Rank, Lower Bound of 90% Confidence Interval", 
"Voice and Accountability: Percentile Rank, Upper Bound of 90% Confidence Interval", 
"Voice and Accountability: Standard Error"), class = "factor"), 
    Indicator.Code = structure(1:10, .Label = c("CC.EST", "CC.NO.SRC", 
    "CC.PER.RNK", "CC.PER.RNK.LOWER", "CC.PER.RNK.UPPER", "CC.STD.ERR", 
    "GE.EST", "GE.NO.SRC", "GE.PER.RNK", "GE.PER.RNK.LOWER", 
    "GE.PER.RNK.UPPER", "GE.STD.ERR", "PV.EST", "PV.NO.SRC", 
    "PV.PER.RNK", "PV.PER.RNK.LOWER", "PV.PER.RNK.UPPER", "PV.STD.ERR", 
    "RL.EST", "RL.NO.SRC", "RL.PER.RNK", "RL.PER.RNK.LOWER", 
    "RL.PER.RNK.UPPER", "RL.STD.ERR", "RQ.EST", "RQ.NO.SRC", 
    "RQ.PER.RNK", "RQ.PER.RNK.LOWER", "RQ.PER.RNK.UPPER", "RQ.STD.ERR", 
    "VA.EST", "VA.NO.SRC", "VA.PER.RNK", "VA.PER.RNK.LOWER", 
    "VA.PER.RNK.UPPER", "VA.STD.ERR"), class = "factor"), X1996 = c(-1.29170477390289, 
    2, 4.30107545852661, 0, 27.4193553924561, 0.340506970882416, 
    -2.17516660690308, 1, 0, 0), X1998 = c(-1.18084812164307, 
    2, 9.79381465911865, 0, 31.4432983398438, 0.330179065465927, 
    -2.13594222068787, 1, 0, 0), X2000 = c(-1.29537987709045, 
    2, 5.07614231109619, 0, 29.4416236877441, 0.356294184923172, 
    -2.23165082931519, 1, 0, 0), X2002 = c(-1.26336598396301, 
    2, 5.0505051612854, 0, 31.8181819915771, 0.359914720058441, 
    -1.56797313690186, 2, 2.04081630706787, 0), X2003 = c(-1.35104191303253, 
    3, 5.0505051612854, 0, 18.1818180084229, 0.275338709354401, 
    -1.08248770236969, 2, 11.7346935272217, 1.5306122303009), 
    X2004 = c(-1.34528136253357, 5, 5.85365867614746, 0, 14.1463413238525, 
    0.214314609766006, -0.894050717353821, 4, 17.2413787841797, 
    6.89655160903931), X2005 = c(-1.4436092376709, 5, 2.43902444839478, 
    0, 13.1707315444946, 0.207736045122147, -1.2108291387558, 
    4, 9.80392169952393, 1.96078431606293), X2006 = c(-1.4328898191452, 
    7, 3.90243911743164, 0, 12.1951217651367, 0.206337913870811, 
    -1.43034291267395, 6, 5.36585378646851, 0), X2007 = c(-1.58733141422272, 
    6, 0.970873773097992, 0, 7.28155326843262, 0.229119420051575, 
    -1.40335166454315, 5, 5.33980560302734, 0.485436886548996
    ), X2008 = c(-1.63828659057617, 7, 0.485436886548996, 0, 
    4.85436916351318, 0.21933402121067, -1.49608886241913, 6, 
    4.36893224716187, 0.485436886548996), X2009 = c(-1.53479599952698, 
    8, 0.956937789916992, 0, 7.65550231933594, 0.201660826802254, 
    -1.48389947414398, 7, 3.82775115966797, 0), X2010 = c(-1.63572347164154, 
    9, 0.952380955219269, 0, 3.80952382087708, 0.195554181933403, 
    -1.4546834230423, 7, 4.78468894958496, 0.478468894958496), 
    X2011 = c(-1.57917881011963, 9, 0.947867274284363, 0, 5.68720388412476, 
    0.194524049758911, -1.44848775863647, 7, 4.7393364906311, 
    0.947867274284363), X2012 = c(-1.41988801956177, 10, 2.36966824531555, 
    0, 11.8483409881592, 0.186807841062546, -1.36013960838318, 
    8, 6.63507127761841, 0.473933637142181), X2013 = c(-1.43676114082336, 
    11, 1.89573454856873, 0, 9.95260620117188, 0.160680562257767, 
    -1.38492977619171, 8, 7.5829381942749, 0.473933637142181), 
    X2014 = c(-1.35478448867798, 11, 5.28846168518066, 0.480769217014313, 
    12.5, 0.161188751459122, -1.33139562606812, 8, 8.17307662963867, 
    1.44230771064758), X2015 = c(-1.34197008609772, 11, 6.25, 
    1.44230771064758, 12.5, 0.162221297621727, -1.32298874855042, 
    8, 8.17307662963867, 1.44230771064758), X2016 = c(-1.56373512744904, 
    10, 3.36538457870483, 0, 8.65384578704834, 0.175009191036224, 
    -1.2221063375473, 8, 9.61538505554199, 4.32692289352417), 
    X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
10L), class = "data.frame")

filter()& mutate()-部分

gg_stack <- dplyr::filter(gg_stack,
                          Indicator.Code %in% c("CC.EST", "GE.EST", "PV.EST", "RL.EST", "RQ.EST", "VA.EST"))

gg_stack <- gg_stack %>% 
select(-Indicator.Name,
     -X1996:-X2015,
     -X)

gg_stack <- gg_stack %>%
  mutate(
    cc2016 = case_when(Indicator.Code == "CC.EST" ~ X2016),
    ge2016 = case_when(Indicator.Code == "GE.EST" ~ X2016),
    pv2016 = case_when(Indicator.Code == "PV.EST" ~ X2016),
    rq2016 = case_when(Indicator.Code == "RQ.EST" ~ X2016),
    rl2016 = case_when(Indicator.Code == "RL.EST" ~ X2016),
    va2016 = case_when(Indicator.Code == "VA.EST" ~ X2016)
  )
dput(head(gg_stack, 10))

输出为dput(head(gg_stack, 10))

> dput(head(gg_stack, 10))
structure(list(Country.Name = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L), .Label = c("Afghanistan", "Albania", "Algeria", 
"American Samoa", "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", 
"Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 
"Bahamas, The", "Bahrain", "Bangladesh", "Barbados", "Belarus", 
"Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 
"Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", 
"Cameroon", "Canada", "Cayman Islands", "Central African Republic", 
"Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Dem. Rep.", 
"Congo, Rep.", "Cook Islands", "Costa Rica", "Cote d'Ivoire", 
"Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", 
"Dominica", "Dominican Republic", "Ecuador", "Egypt, Arab Rep.", 
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", 
"Fiji", "Finland", "France", "French Guiana", "Gabon", "Gambia, The", 
"Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", 
"Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", 
"Honduras", "Hong Kong SAR, China", "Hungary", "Iceland", "India", 
"Indonesia", "Iran, Islamic Rep.", "Iraq", "Ireland", "Israel", 
"Italy", "Jamaica", "Japan", "Jersey, Channel Islands", "Jordan", 
"Kazakhstan", "Kenya", "Kiribati", "Korea, Dem. People’s Rep.", 
"Korea, Rep.", "Kosovo", "Kuwait", "Kyrgyz Republic", "Lao PDR", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", 
"Lithuania", "Luxembourg", "Macao SAR, China", "Macedonia, FYR", 
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
"Marshall Islands", "Martinique", "Mauritania", "Mauritius", 
"Mexico", "Micronesia, Fed. Sts.", "Moldova", "Monaco", "Mongolia", 
"Montenegro", "Morocco", "Mozambique", "Myanmar", "Namibia", 
"Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "Niue", "Norway", "Oman", "Pakistan", 
"Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", 
"Russian Federation", "Rwanda", "Samoa", "San Marino", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovak Republic", "Slovenia", "Solomon Islands", 
"Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", 
"St. Kitts and Nevis", "St. Lucia", "St. Vincent and the Grenadines", 
"Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan, China", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", 
"Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", 
"Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", 
"United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", 
"Venezuela, RB", "Vietnam", "Virgin Islands (U.S.)", "West Bank and Gaza", 
"Yemen, Rep.", "Zambia", "Zimbabwe"), class = "factor"), Country.Code = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L), .Label = c("ABW", "AFG", 
"AGO", "AIA", "ALB", "AND", "ANT", "ARE", "ARG", "ARM", "ASM", 
"ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", 
"BGR", "BHR", "BHS", "BIH", "BLR", "BLZ", "BMU", "BOL", "BRA", 
"BRB", "BRN", "BTN", "BWA", "CAF", "CAN", "CHE", "CHL", "CHN", 
"CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPV", "CRI", 
"CUB", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA", "DNK", "DOM", 
"DZA", "ECU", "EGY", "ERI", "ESP", "EST", "ETH", "FIN", "FJI", 
"FRA", "FSM", "GAB", "GBR", "GEO", "GHA", "GIN", "GMB", "GNB", 
"GNQ", "GRC", "GRD", "GRL", "GTM", "GUF", "GUM", "GUY", "HKG", 
"HND", "HRV", "HTI", "HUN", "IDN", "IND", "IRL", "IRN", "IRQ", 
"ISL", "ISR", "ITA", "JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", 
"KGZ", "KHM", "KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBR", 
"LBY", "LCA", "LIE", "LKA", "LSO", "LTU", "LUX", "LVA", "MAC", 
"MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", 
"MLT", "MMR", "MNE", "MNG", "MOZ", "MRT", "MTQ", "MUS", "MWI", 
"MYS", "NAM", "NER", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL", 
"NRU", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", "PLW", "PNG", 
"POL", "PRI", "PRK", "PRT", "PRY", "PSE", "QAT", "REU", "ROU", 
"RUS", "RWA", "SAU", "SDN", "SEN", "SGP", "SLB", "SLE", "SLV", 
"SMR", "SOM", "SRB", "SSD", "STP", "SUR", "SVK", "SVN", "SWE", 
"SWZ", "SYC", "SYR", "TCD", "TGO", "THA", "TJK", "TKM", "TLS", 
"TON", "TTO", "TUN", "TUR", "TUV", "TWN", "TZA", "UGA", "UKR", 
"URY", "USA", "UZB", "VCT", "VEN", "VIR", "VNM", "VUT", "WSM", 
"XKX", "YEM", "ZAF", "ZMB", "ZWE"), class = "factor"), Indicator.Code = structure(c(1L, 
7L, 13L, 25L, 19L, 31L, 1L, 7L, 13L, 25L), .Label = c("CC.EST", 
"CC.NO.SRC", "CC.PER.RNK", "CC.PER.RNK.LOWER", "CC.PER.RNK.UPPER", 
"CC.STD.ERR", "GE.EST", "GE.NO.SRC", "GE.PER.RNK", "GE.PER.RNK.LOWER", 
"GE.PER.RNK.UPPER", "GE.STD.ERR", "PV.EST", "PV.NO.SRC", "PV.PER.RNK", 
"PV.PER.RNK.LOWER", "PV.PER.RNK.UPPER", "PV.STD.ERR", "RL.EST", 
"RL.NO.SRC", "RL.PER.RNK", "RL.PER.RNK.LOWER", "RL.PER.RNK.UPPER", 
"RL.STD.ERR", "RQ.EST", "RQ.NO.SRC", "RQ.PER.RNK", "RQ.PER.RNK.LOWER", 
"RQ.PER.RNK.UPPER", "RQ.STD.ERR", "VA.EST", "VA.NO.SRC", "VA.PER.RNK", 
"VA.PER.RNK.LOWER", "VA.PER.RNK.UPPER", "VA.STD.ERR"), class = "factor"), 
    X2016 = c(-1.56373512744904, -1.2221063375473, -2.74916744232178, 
    -1.32791936397552, -1.61642956733704, -1.08621954917908, 
    -0.397567093372345, -0.000573236728087068, 0.257720142602921, 
    0.188962236046791), cc2016 = c(-1.56373512744904, NA, NA, 
    NA, NA, NA, -0.397567093372345, NA, NA, NA), ge2016 = c(NA, 
    -1.2221063375473, NA, NA, NA, NA, NA, -0.000573236728087068, 
    NA, NA), pv2016 = c(NA, NA, -2.74916744232178, NA, NA, NA, 
    NA, NA, 0.257720142602921, NA), rq2016 = c(NA, NA, NA, -1.32791936397552, 
    NA, NA, NA, NA, NA, 0.188962236046791), rl2016 = c(NA, NA, 
    NA, NA, -1.61642956733704, NA, NA, NA, NA, NA), va2016 = c(NA, 
    NA, NA, NA, NA, -1.08621954917908, NA, NA, NA, NA)), row.names = c(NA, 
10L), class = "data.frame")

最后我得到这个data.frame:

> str(gg_stack)
'data.frame':   1284 obs. of  10 variables:
 $ Country.Name  : Factor w/ 214 levels "Afghanistan",..: 1 1 1 1 1 1 2 2 2 2 ...
 $ Country.Code  : Factor w/ 214 levels "ABW","AFG","AGO",..: 2 2 2 2 2 2 5 5 5 5 ...
 $ Indicator.Code: Factor w/ 36 levels "CC.EST","CC.NO.SRC",..: 1 7 13 25 19 31 1 7 13 25 ...
 $ X2016         : num  -1.56 -1.22 -2.75 -1.33 -1.62 ...
 $ cc2016        : num  -1.56 NA NA NA NA ...
 $ ge2016        : num  NA -1.22 NA NA NA ...
 $ pv2016        : num  NA NA -2.75 NA NA ...
 $ rq2016        : num  NA NA NA -1.33 NA ...
 $ rl2016        : num  NA NA NA NA -1.62 ...
 $ va2016        : num  NA NA NA NA NA ...

如您所见,我为每个国家/地区获得了 6 行。现在,每个指标列都是一个单独的行。对于指标 cc2016,我得到了第一行的值,接下来的五行是 NA。那么,我怎样才能将我的 data.frame 压缩成这样的格式(每个指标列都有相同的行):

 $ X2016         : num  -1.56 -1.22 -2.75 -1.33 -1.62 ...
 $ cc2016        : num  -1.56 ...
 $ ge2016        : num  -1.22 ...
 $ pv2016        : num  -2.75 ...
 $ rq2016        : num  -1.33 ...
 $ rl2016        : num  -1.62 ...
 $ va2016        : num   ...

我需要更改变异部分中的代码吗?我真的希望你能解决我的问题。

标签: rdataframedplyr

解决方案


推荐阅读