简体   繁体   中英

Merging two formatted .csv files by row name

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM