[R] How to join data.frames with different row lengths

arun smartpink111 at yahoo.com
Mon Mar 24 19:42:49 CET 2014


Hi,
Try ?merge() or ?join from library(plyr)
If `dat1` and `dat2` are the datasets:
res <- merge(dat1,dat2,by="Code",all=TRUE)
 res[is.na(res)] <- 0
 res
#  Code Cap04 Cap08
#1    2   120   120
#2    6    75     0
#3    7   220   112
#4    9     0   190
#5   17     4     4
A.K.



Hello, 

I need to combine two data sets into one. For example: 

Dataset1: 
Code   Cap04   
2        120         
6          75         
7        220         
17         4         

Dataset2: 
Code   Cap08   
2        120         
7        112         
9        190         
17         4         


I need the dataset to look like the following where it keeps
 every unique 'code' from both datasets and fills the blanks in both 
rows with 0: 

FinalDataSet: 
Code   Cap04   Cap08 
2         120       120 
6          75          0 
7         220       112 
9            0       190 
17          4          4 

Can anyone help me with this please? 

Thank you very much! 
Johnson



More information about the R-help mailing list