-1

I am looking for a way to combine multiple files with unequal row lengths. Each file has 2 columns, of which the first is the row name, and the second an output-value (number). I have many different files of this type, and I want to merge these based on the row name. The file name should be the second, third, etcetera, column name.

Example

file1
#results of experiment 1
outcome1 15
outcome2 2
outcome3 1008

file2
#results of experiment 2
outcome1 440
outcome2 76
outcome3 4324
outcome4 873

Expected output:

         file1 file2    
outcome1 15    440
outcome2 2     76
outcome3 1008  4324
outcome4 N/A   873

Thanks in advance for your suggestions!

Alexander

  • 1
    Duplication of this question: https://stackoverflow.com/questions/6029743/merge-or-combine-by-rownames – ozturkib Jun 05 '20 at 08:42

1 Answers1

0

Suppose your columns for each file are named row_name and value and your data are stored in data.frames named file1 and file2. Then

df <- merge(file1, file2, by="row_name", all=TRUE)

gives you

> df
  row_name number.x number.y
1 outcome1       15      440
2 outcome2        2       76
3 outcome3     1008     4324
4 outcome4       NA      873

It's easy to rename the columns in your new data.frame with

names(df) <- c("row_names", "file1", "file2")
Martin Gal
  • 16,640
  • 5
  • 21
  • 39