0

Sorry I'm struggling with some of the basics of R.

I have several datasets I want to merge into one dataframe. I want to add variables "Recovered", "NewTests", "Population", "GDP" and "GDPCapita" from:

dat1

Country     Date        Recovered
  <fct>       <chr>           <int>
1 Afghanistan X2020.01.22         0
2 Afghanistan X2020.01.23         0
3 Afghanistan X2020.01.24         0
4 Afghanistan X2020.01.25         0
5 Afghanistan X2020.01.26         0
6 Afghanistan X2020.01.27         0

dat2

Code       Date NewTests
1  ARG 2020-03-04        0
2  ARG 2020-03-06        0
3  ARG 2020-03-07        0
4  ARG 2020-03-08        0
5  ARG 2020-03-09        0
6  ARG 2020-03-11        0

dat3

Code     Country Population    GDP GDPCapita
1  AFG Afghanistan   37172386  21992       619
2  ALB     Albania    2866376  13039      4450
3  DZA     Algeria   42228429 167555      4055
4  AND     Andorra      77006   3278     39153
5  AGO      Angola   30809762 126505      4247
6  AIA    Anguilla      14731    311     29493

To a master dataframe of:

master

 Code     Country Continent       Date NewCases NewDeaths
1  AFG Afghanistan      Asia 2020-01-01        0         0
2  AFG Afghanistan      Asia 2020-01-02        0         0
3  AFG Afghanistan      Asia 2020-01-03        0         0
4  AFG Afghanistan      Asia 2020-01-04        0         0
5  AFG Afghanistan      Asia 2020-01-05        0         0
6  AFG Afghanistan      Asia 2020-01-06        0         0

To give the following output of:

[enter image description here][1]

I did attempt merge(master, dat1, dat2, dat3, by = c('Country')) but received the following warning

  'by' must specify one or more columns as numbers, names or logical ```

Any tips would be greatly appreciated!


  [1]: https://i.stack.imgur.com/G7uHt.png
Jaz
  • 5
  • 2

1 Answers1

1

You can only merge two dataframes together at a time. Moreover, make sure the columns that you want to merge are of the same class and same format.

library(magrittr)
dat1$Date <- as.Date(dat1$Date, 'X%Y.%m.%d')

merge(master, dat1, by = c('Country', 'Date')) %>%
   merge(dat2, by = c('Code', 'Date')) %>%
    merge(dat3, by = c('Code', 'Country')) -> result
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213