[R] matrix/df help populate NA

Adrian Johnson oriolebaltimore at gmail.com
Sun Jun 14 05:17:35 CEST 2015


Dear group:

I have two data frames. The column names of the two data frame has
some common variables but not identical.

my aim is to make 2  DFs more uniform by taking union of both colnames


For example: I have x1 and x2 matrices:

> x1
  Subject    A    B   C    D
1      x1  1.5 -1.3 0.4 -0.2
2      x2 -1.2 -0.3 0.3 -0.1
> x2
  Subject   A    D   F    H
1      x1 4.3 -2.4 1.3 -2.3
2      x2 2.4  0.1 0.5 -1.4

 cases = c('A','B','C','D','F','H')

for X2 I want to create newX2 DF.

> x3
  Subject   A  B  C    D   F    H
1      x1 4.3 NA NA -2.4 1.3 -2.3
2      x2 2.4 NA NA  0.1 0.5 -1.4


Since B and C are no existing in x2, I put NAs.

how can I create x3 matrix?



dput code:

x1 = structure(list(Subject = c("x1", "x2"), A = c(1.5, -1.2), B = c(-1.3,
-0.3), C = c(0.4, 0.3), D = c(-0.2, -0.1)), .Names = c("Subject",
"A", "B", "C", "D"), class = "data.frame", row.names = c(NA,
-2L))

x2 = structure(list(Subject = c("x1", "x2"), A = c(4.3, 2.4), D = c(-2.4,
0.1), F = c(1.3, 0.5), H = c(-2.3, -1.4)), .Names = c("Subject",
"A", "D", "F", "H"), class = "data.frame", row.names = c(NA,
-2L))


Could you please help how to create x3 with NAs incorporated.
adrian.



More information about the R-help mailing list