-2

i have a small (2k) dataset. each row has its own unique id under "id" column. from another software i get a subset of this data with values only to some rows. i now need to find a way to insert each value to correct id and set NA in the other values. here is a simple example:

data = data.frame(id =1:10, b= rep(c("boy", "girl"), each = 5))
sub_data = data frame(id = c(1,4,7,8), value = c( 0.1,0.3,0.15,0.9))

and the answer should look like:

final_data = data.frame(id =1:10, b= rep(c("boy", "girl"), each = 5), values = c(0.1, "NA", "NA", 0.3, "NA", "NA", 0.15, 0.9, "NA", "NA"))

thank you for helping!

isomitzi
  • 131
  • 9

1 Answers1

0

Try merge

 merge(data, sub_data, all.x=TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662