【问题标题】:R: Expand year range for some countries in dataframeR:扩大数据框中某些国家/地区的年份范围
【发布时间】:2021-07-20 16:18:44
【问题描述】:

我有以下问题:我有一个所有北约国家的数据集,其中包括 1980 年至 2020 年的年度成员资格。现在我想包括北约成立以来的所有年份(1949-2020)。例如,对于比利时、加拿大、丹麦、法国、冰岛、意大利、卢森堡、荷兰、挪威、葡萄牙、英国、美国,应该添加 1949-1980 年的行。

这是数据集的简化版本:

country <- rep(c("Belgium", "Albania"), each= 10)
countrycode <- rep(c(56, 8), each=10)
year <- rep(c(1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000), 2)
   
df <- data.frame(country, countrycode, year)

   country countrycode year
1  Belgium          56 1990
2  Belgium          56 1991
3  Belgium          56 1992
4  Belgium          56 1994
5  Belgium          56 1995
6  Belgium          56 1996
7  Belgium          56 1997
8  Belgium          56 1998
9  Belgium          56 1999
10 Belgium          56 2000
11 Albania           8 1990
12 Albania           8 1991
13 Albania           8 1992
14 Albania           8 1994
15 Albania           8 1995
16 Albania           8 1996
17 Albania           8 1997
18 Albania           8 1998
19 Albania           8 1999
20 Albania           8 2000

在这个例子中,我如何最有效地将比利时的年份延长到 1949 年,同时让阿尔巴尼亚保持原样?

提前感谢您的回答!

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    您可以使用 expand.grid 创建所有有效的年份和国家组合(我认为这里的数据足够小)。然后将所有组合与现有数据进行左连接。这会为国家和年份的每个组合生成一行。

    country <- rep(c("Belgium", "Albania"), each= 10)
    
    allCombos <- expand.grid(unique(country), 1949:2020) %>%
        setNames(., c('country', 'year'))
    
    countrycode <- rep(c(56, 8), each=10)
    year <- rep(c(1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000), 2)
       
    df <- data.frame(country, countrycode, year)
    
    merge(allCombos, df, by = c('country', 'year'), all.x=TRUE)
    

    部分输出:

        country year countrycode
    1   Belgium 1949          NA
    2   Belgium 1950          NA
    3   Belgium 1951          NA
    4   Belgium 1952          NA
    5   Belgium 1953          NA
    6   Belgium 1954          NA
    7   Belgium 1955          NA
    8   Belgium 1956          NA
    9   Belgium 1957          NA
    10  Belgium 1958          NA
    11  Belgium 1959          NA
    12  Belgium 1960          NA
    

    【讨论】:

      【解决方案2】:

      我们可以用比利时 1949 - 1989 创建一个数据框,然后使用bind_rows

      country <- "Belgium"
      countrycode <- 56
      year <- 1949:1989
      df1 <- data.frame(country, countrycode, year)
      
      library(dplyr)
      bind_rows(df, df1) %>% 
        arrange(country, year)
      

      输出

      country countrycode year
      1  Albania           8 1990
      2  Albania           8 1991
      3  Albania           8 1992
      4  Albania           8 1994
      5  Albania           8 1995
      6  Albania           8 1996
      7  Albania           8 1997
      8  Albania           8 1998
      9  Albania           8 1999
      10 Albania           8 2000
      11 Belgium          56 1949
      12 Belgium          56 1950
      13 Belgium          56 1951
      14 Belgium          56 1952
      15 Belgium          56 1953
      16 Belgium          56 1954
      17 Belgium          56 1955
      18 Belgium          56 1956
      19 Belgium          56 1957
      20 Belgium          56 1958
      21 Belgium          56 1959
      22 Belgium          56 1960
      23 Belgium          56 1961
      24 Belgium          56 1962
      25 Belgium          56 1963
      26 Belgium          56 1964
      27 Belgium          56 1965
      28 Belgium          56 1966
      29 Belgium          56 1967
      30 Belgium          56 1968
      31 Belgium          56 1969
      32 Belgium          56 1970
      33 Belgium          56 1971
      34 Belgium          56 1972
      35 Belgium          56 1973
      36 Belgium          56 1974
      37 Belgium          56 1975
      38 Belgium          56 1976
      39 Belgium          56 1977
      40 Belgium          56 1978
      41 Belgium          56 1979
      42 Belgium          56 1980
      43 Belgium          56 1981
      44 Belgium          56 1982
      45 Belgium          56 1983
      46 Belgium          56 1984
      47 Belgium          56 1985
      48 Belgium          56 1986
      49 Belgium          56 1987
      50 Belgium          56 1988
      51 Belgium          56 1989
      52 Belgium          56 1990
      53 Belgium          56 1991
      54 Belgium          56 1992
      55 Belgium          56 1994
      56 Belgium          56 1995
      57 Belgium          56 1996
      58 Belgium          56 1997
      59 Belgium          56 1998
      60 Belgium          56 1999
      61 Belgium          56 2000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 2018-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多