0

I have two data frames: "bim2"

enter image description here

and "illum".

enter image description here

If V2 of bim2 match with illum V1 (match(bim2$V2, illum$V1)), I want to replace bim$V2 with the value in illum$V2. If it doesn't match (bim2$V2 not equal to illum$V1), I want to keep the original values in bim$V2.

I've used

bim2$V2 <- illum$V2[match(bim2$V2, illum$V1)]

but it replaces the "bim2$V2 not equal to illum$V1" by NAs.

  • Are they the same size? Could you provide a sample? – akash87 Jun 22 '16 at 11:37
  • Please take a second read through your question. you say your data.frame is bim, but you are using bim2 in your code. Also make sure that the description of the replacement is correct. It does not line up with your `match` code: `illum$v1` for example. – lmo Jun 22 '16 at 11:38
  • Welcome to StackOverflow! This will help you write great R questions http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jun 22 '16 at 11:53
  • @akash87 same size, thanks – Julio Rodríguez Jun 22 '16 at 13:13

1 Answers1

0

Modifying your given code, you can do :

bim2$V2[match(bim2$V7, illum$V1)] <- illum$V2[match(bim2$V7, illum$V1)]

It will replace the values that match, and keep the rest of the values. I hope, that works

Virag Swami
  • 197
  • 13
  • @GorrilleiFalciani It will keep the values that doesn't match and replace only the one when bim2$V7 and illum$V1 match – Virag Swami Jun 23 '16 at 06:41