0

I've got different data frame with this kind of data.

DF1
Name    |   Accession  | Character | %    |
P07437  |  beta        |  SJ       | 55   |
P07137  |  alpha       |  SA       | 66   |
P07677  |  beta        |  SI       | 65   |
P07437  |  alpha       |  ST       | 12   |

DF2
Name    |   Accession  | Character | %    |
P0225  |  beta        |  SJ       | 55   |
P0757  |  alpha       |  SA       | 66   |
P0377  |  beta        |  SI       | 65   |
P0137  |  alpha       |  ST       | 12   |

DF3
Name    |   Accession  | Character | %    |
P0357  |  beta        |  SK       | 55   |
P1540  |  beta       |  SA       | 66   |
P2367  |  gamma        |  SI       | 65   |
P6985  |  alpha       |  ST      | 12   |

DF4
Name    |   Accession  | Character | %    |
P07437  |  beta        |  SJ       | 55   |
P07137  |  beta       |  SA       | 66   |
P07677  |  alpha       |  SI       | 65   |
P07437  |  alpha       |  ST       | 12   |

When I try to use merge function with two data.frame the output is correct:

df_final <- merge(df1, df2  by=c("Name", "Accession"),all=TRUE)

But when I try to use merge function with 3 or more data frames I got this Error:

df_final <- merge(df1, df2, df3, df4,  by=c("Name", "Accession"),all=TRUE)


Error in fix.by(by.x, x) : 
  'by' must specify one or more columns as numbers, names or logical

Is there any way to use merge function with multiple data frames?

Here you are a reproducible example:

df1 <- data.frame(Name=c("P07137","P07677","P07437"), Accession=c("alpha","beta","beta"), Character=c("SJ","SA","SS"), Percentage=c(5,10,25))

df2 <- data.frame(Name=c("P0225","P0757","P0337"), Accession=c("alpha","beta","beta"), Character=c("SJ","SA","SS"), Percentage=c(5,10,25))

df3 <- data.frame(Name=c("P02137","P17677","P87437"), Accession=c("alpha","alpha","beta"), Character=c("SU","SS","ST"), Percentage=c(5,10,25))

df4 <- data.frame(Name=c("P0057","P07677","P07437"), Accession=c("alpha","beta","beta"), Character=c("SJ","SA","SS"), Percentage=c(5,10,25))

Thank you in advance.

`

Enrique
  • 842
  • 1
  • 9
  • 21
  • You could use `Reduce` to do it: `df_final <- Reduce(function(x, y) merge (x, y, by = c("Name", "Accession"), all = TRUE), list(df1, df2, df3, df4))`. You get a warning though because the column names are all the same in the data frames. – ikop Apr 10 '17 at 08:08
  • I'm confused because all data frames have the same structure, same column names, etc.. Perhaps, you want to append ("union" in SQL) the pieces with `rbind` or `rbindlist` rather than merging/joining? Please, show an example of your expected result. – Uwe Apr 10 '17 at 08:38

0 Answers0