mardi 1 décembre 2015

conditional loop in R dataframe

I want to write a loop where in if the value in columns 2:4 is "NA", the corresponding values in columns 5:7 will turn to N

My sample data is as follows:

ID SA1.x SA2.x SA3.x SA1.y SA2.y SA3.y

M1_155 15 25 18 C C C

M1_206 12 NA 34 K T K

M1_502 39 42 20 A A A

M1_519 NA NA 11 T T C

M1_59 NA 16 30 N M M

M1_68 60 32 82 R R R

M2_134 23 16 19 R G G

M2_174 27 NA NA G G G

M2_206 86 67 107 K T K

M2_502 NA NA NA A A A

M2_59 NA 28 36 N M M

M2_68 18 14 19 R R R

My desired output is::

ID SA1.x SA2.x SA3.x SA1.y SA2.y SA3.y

M1_155 15 25 18 C C C

M1_206 12 NA 34 K N K

M1_502 39 42 20 A A A

M1_519 NA NA 11 N N C

M1_59 NA 16 30 N M M

M1_68 60 32 82 R R R

M2_134 23 16 19 R G G

M2_174 27 NA NA G N N

M2_206 86 67 107 K T K

M2_502 NA NA NA N N N

M2_59 NA 28 36 N M M

M2_68 18 14 19 R R R

or the final three columns with the digits modified.

Thanks in advance. J

Aucun commentaire:

Enregistrer un commentaire