im going to explain to you my question on base of the sample data. Here is first table (df1):
x x1 y z
1 1 10 a 11
2 3 11 b 13
3 5 10 c 15
4 7 11 d 17
5 9 10 e 19
here is a dput()
version:
structure(list(x = c(1, 3, 5, 7, 9), x1 = c(10, 11, 10, 11, 10
), y = structure(1:5, .Label = c("a", "b", "c", "d", "e"), class = "factor"),
z = c(11, 13, 15, 17, 19)), .Names = c("x", "x1", "y", "z"
), row.names = c(NA, -5L), class = "data.frame")
and second table (df2):
x x1
1 2 10
2 3 60
dput()
:
structure(list(x = c(2, 3), x1 = c(10, 60)), .Names = c("x",
"x1"), row.names = c(NA, -2L), class = "data.frame")
I need to now bind rows of these two tables and fill the missing column values with values from df1. Let me explain you on base of these two tables.
At first i use smartbind()
function from gtools
library:
library(gtools)
data <- smartbind(df1, df2)
And the result that i get looks like that:
x x1 y z
1 10 a 11
3 11 b 13
5 10 c 15
7 11 d 17
9 10 e 19
2 10 <NA> NA
3 60 <NA> NA
So i would like to fill up the all NA values which appear in the rows from df2, with df1 values if the x is the same. In this case it would look like that:
x x1 y z
1 10 a 11
3 11 b 13
5 10 c 15
7 11 d 17
9 10 e 19
2 10 <NA> NA
3 60 b 13
In my original dataset i do have around 280 columns! Thanks for help
Is there any more ELEGANT way to do it rather then joining two data frames and then using rbind()