1

I'm trying to insert values from one dataset A column x that has 23 less rows to another dataset B with the same column x but filled with NA. Both datasets have column "id" and I want to fill empty column with values where id's from both datasets match and with "None" where rows are missing. I created column results to not mess up my outputs (id is integer and x is a factor).

So far I have this: $

A <- data.frame (id  = c("1", "11", "2"),
                  x = c("123", "420", "3"))
B <- data.frame (id  = c("1", "11", "2", "5", "6"),
                 x = c("NA", "NA", "NA", "NA", "NA"))

A$results = ifelse(A$id == B$id, insert(A$x),
                               ifelse(A$id != B$id, 'None')) 

$ but there is an error: In A$id == B$id : longer object length is not a multiple of shorter object length

Missing values are not at the end of the dataset.

dataset A dataset BWhat I want to get

Big_blob
  • 11
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It sounds like maybe you just want a jeft-join type merge. – MrFlick Jan 31 '23 at 15:38
  • Sounds like you want `dplyr::rows_update(A, B, by = "id")` (or maybe `rows_patch()`?) but it's hard to tell with just pictures. – Gregor Thomas Jan 31 '23 at 16:12

0 Answers0