Wednesday, April 18, 2012

Augment data frame missed values by another data frame

I have following data frames:



> df1 = data.frame(ind = 1:4, x=c('a', 'b', NA, 'd'))
> df2 = data.frame(ind = 1:4, x=c(NA, NA, 'c', NA))
> df1
ind x
1 1 a
2 2 b
3 3 <NA>
4 4 d
> df2
ind x
1 1 <NA>
2 2 <NA>
3 3 c
4 4 <NA>


I want combine them filling missing values in df1 by numeric values from df2. How can I do that? I cannot do that neither with merge nor with join commands:



> merge(df1, df2, by='ind', all=T)
ind x.x x.y
1 1 a <NA>
2 2 b <NA>
3 3 <NA> c
4 4 d <NA>




No comments:

Post a Comment