r - Replace different values in one column, according to the row information in another column -
i working genomic data, , have 1 data frame, going show first 3 rows (see table below):
chrom | pos | id | ref | alt | hapa | hapb | ---------------------------------------------------------- 22 | 16495833 | rs116911124 | | c | 1 | 0 | 22 | 19873357 | rs116378360 | t | | 0 | 1 | 22 | 21416404 | rs117982183 | t | t | 0 | . |
so, replace values of "0", "1" , "." "hapa" , "hapb" columns according ref , alt columns every row in data frame. example:
a) first row want change "1" in hapa column "c" in alt column, , "0" in hapb column "a" value in ref column
b) second row change "0" "t" in "ref" column , "1" "a" in "alt" column.
c) , finally, "." change "na"
i think achieved using "if else" or data.table.
thank much.
it's bit unclear want exactly, since don't specify should happen 0
in third row of hapa
column, given said, dplyr
solution:
library(dplyr) df <- read.table(text = " 'chrom' 'pos' 'id' 'ref' 'alt' 'hapa' 'hapb' 22 16495833 'rs116911124' 'a' 'c' 1 0 22 19873357 'rs116378360' 't' 'a' 0 1 22 21416404 'rs117982183' 't' 't' 0 .", header = t, stringsasfactors = f) df %>% mutate(hapa = ifelse(hapa == 1, alt, ifelse(hapa == 0, ref, na)), hapb = ifelse(hapb == 1, alt, ifelse(hapb == 0, ref, na))) ## chrom pos id ref alt hapa hapb ## 1 22 16495833 rs116911124 c c ## 2 22 19873357 rs116378360 t t ## 3 22 21416404 rs117982183 t t t <na>
Comments
Post a Comment