0

Aamazingly, I had tried three different kind of codes to merge two of my data by two variables. I also checked type of datas and variables, and sure that they are all same. Here the codes that I used and did not work for merge.

First code:

merge(data1,data2, 
    by.x = c(data1$Country, data1$Year), 
    by.y = c(data2$Country, data2$Year))

Second code:

merge(data1,data2, 
    by = c("Country", "Year"))

Third code:

merge(data1, data2, by.x="Country", by.y = "Year")

I got this error from all these codes:

Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

Any help would be appreciated.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
FGH
  • 91
  • 3
  • 8

1 Answers1

1

First off, you should review How to make a great R reproducible example, specifically the advice on what to do when you cannot share your original data.

Since we have no idea how your data actually look like it is difficult to provide specific help. That said, in order to work towards a solution, I simulate some mock data below and show how to merge two data.frames based on entries in two columns:

set.seed(2018)
data1 <- data.frame(
    Country = sample(letters, 10),
    Year = sample(1:4, 10, replace = T),
    Value = sample(10))

data2 <- data.frame(
    Country = sample(letters, 10),
    Year = sample(1:4, 10, replace = T),
    Value = sample(10))

merge(data1, data2, by = c("Country", "Year"))
#  Country Year Value.x Value.y
#1       l    3       6       6

Or if you want to retain rows from each data.frame that don't have a match

merge(data1, data2, by = c("Country", "Year"), all = TRUE)
#   Country Year Value.x Value.y
#1        b    4       2      NA
#2        c    3       9      NA
#3        c    4      NA       3
#4        e    3       1      NA
#5        g    3      10      NA
#6        i    2       3      NA
#7        j    4       7      NA
#8        k    1      NA       1
#9        k    4       5      NA
#10       l    3       6       6
#11       m    2       8      NA
#12       r    3       4      NA
#13       a    3      NA       9
#14       d    4      NA       5
#15       q    1      NA      10
#16       t    1      NA       7
#17       v    4      NA       2
#18       w    3      NA       4
#19       x    4      NA       8
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • @FGH No please don't share links to third-party file hosters. Most SO users (including myself) are loath to download files from random dropbox/GoogleDrive/OneDrive/etc. links. It is important to include sample data in your original post ([edit](https://stackoverflow.com/posts/54306870/edit) your post). Either share (part of) your data using `dput` or include code to generate representative & minimal data. – Maurits Evers Jan 22 '19 at 11:50