1

I have two data frames:

x <- structure(list(Cluster1 = c(53L, NA, NA), Cluster10 = c(48L, 
46L, NA), Cluster11 = c(2L, NA, NA)), row.names = c("Cluster1", 
"Cluster10", "Cluster11"), class = "data.frame")

y <- tructure(list(Cluster1 = c(53L, NA), Cluster10 = c(46L, NA), 
    Cluster11 = c(2L, NA)), row.names = c("Cluster1", "Cluster11"
), class = "data.frame")

It looks like this:

> x
          Cluster1 Cluster10 Cluster11
Cluster1        53        48         2
Cluster10       NA        46        NA
Cluster11       NA        NA        NA

> y
          Cluster1 Cluster10 Cluster11
Cluster1        53        46         2
Cluster11       NA        NA        NA

Now, y miss a row in x that is Cluster10. How can I insert that missing row and fill it with value NAs. The final desired output for y is:

          Cluster1 Cluster10 Cluster11
Cluster1        53        48         2
Cluster10       NA        NA        NA
Cluster11       NA        NA        NA
littleworth
  • 4,781
  • 6
  • 42
  • 76

1 Answers1

3

This is an option:

y[setdiff(row.names(x), row.names(y)),] <- NA

          Cluster1 Cluster10 Cluster11
Cluster1        53        46         2
Cluster11       NA        NA        NA
Cluster10       NA        NA        NA
Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34