在R中合并并填充不同长度的数据

时间:2018-02-17 19:22:35

标签: r dataframe factors tibble data-transform

我使用R并需要合并不同长度的数据

遵循此数据集

> means2012
 # A tibble: 232 x 2
   exporter    eci
   <fct>     <dbl>
 1 ABW       0.235
 2 AFG      -0.850
 3 AGO      -1.40 
 4 AIA       1.34 
 5 ALB      -0.480
 6 AND       1.22 
 7 ANS       0.662
 8 ARE       0.289
 9 ARG       0.176
 10 ARM       0.490
 # ... with 222 more rows

> means2013
 # A tibble: 234 x 2
    exporter     eci
    <fct>      <dbl>
  1 ABW       0.534 
  2 AFG      -0.834 
  3 AGO      -1.26  
  4 AIA       1.47  
  5 ALB      -0.498 
  6 AND       1.13  
  7 ANS       0.616 
  8 ARE       0.267 
  9 ARG       0.127 
 10 ARM       0.0616
 # ... with 224 more rows


> str(means2012)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   232 obs. of  2 variables:
 $ exporter: Factor w/ 242 levels "ABW","AFG","AGO",..: 1 2 3 4 5 6 7 9 10 11 ...
 $ eci     : num  0.235 -0.85 -1.404 1.337 -0.48 ...
> str(means2013)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   234 obs. of  2 variables:
 $ exporter: Factor w/ 242 levels "ABW","AFG","AGO",..: 1 2 3 4 5 6 7 9 10 11 ...
 $ eci     : num  0.534 -0.834 -1.263 1.471 -0.498 ...

请注意,2 tibble有不同的长度。 &#34;出口&#34;是国家。

有没有办法合并两个tibble,查看因素(Exporter)并用&#34; na&#34;填充缺失的内容?

无论是tibble,数据帧还是其他类型,都无关紧要。

像这样:

tibble 1
a 5
b 10
c 15
d 25

tibble 2
a 7
c 23
d 20

merged one:
a 5  7 
b 10 na
c 15 23
d 25 20

2 个答案:

答案 0 :(得分:1)

使用merge,参数all设置为TRUE

tibble1 <- read.table(text="
x y
a 5
b 10
c 15
d 25",header=TRUE,stringsAsFactors=FALSE)

tibble2 <- read.table(text="
x z
a 7
c 23
d 20",header=TRUE,stringsAsFactors=FALSE)


merge(tibble1,tibble2,all=TRUE)

  x  y  z
1 a  5  7
2 b 10 NA
3 c 15 23
4 d 25 20

dplyr::full_join(tibble1,tibble2)效果相同

答案 1 :(得分:0)

您可以重命名列以加入它们,并获取NA,其中缺少其他值。

library(tidyverse)

means2012 %>% 
  rename(eci2012 = eci) %>% 
  full_join(means2013 %>% 
              rename(eci2013 = eci))

但更简洁的方法是添加year列,保持列eci不变,然后将行绑定在一起。

means2012 %>% 
  mutate(year = 2012) %>% 
  bind_rows(means2013 %>% 
              mutate(year = 2013))
相关问题