r - Translate column values based on string match -
i have dataframe called mydf. mydf has values in every column either 1 letter or 2 letters. want replace values 0/0 if 1 of zero.zero vector elements , 0/1 if 1 of elements zero.one. , nothing if na or other character combinations.
zero.zero<-c("a","t","g","c") zero.one <-c("ag","at","ac","ga","gc","gt","ta","tc","tg","ct","cg","ca") code this: translation <-function(x){ if (mydf[,x] == zero.zero){ mydf[,x]<-"0/0" }else{ if (mydf[,x]==zero.one)){ mydf[,x]<-"0/1" } } } mydf ap bp cp @ ga gt ag g result ap bp cp 0/0 0/1 0/1 0/0 0/1 0/0 0/1 0/0
you can try:
tab<-stack(list("0/0"=zero.zero,"0/1"=zero.one)) mydf[]<-lapply(mydf,function(x) tab$ind[match(x,tab$value)]) # ap bp cp #1 0/0 0/1 0/1 #2 0/0 <na> 0/1 #3 0/0 0/1 0/0 with first line create lookup table in each element of zero.zero , zero.one coupled desired replacement. then, in second line perform same operation on each column of mydf through lapply. operation consists in finding each element of column stands in column values of our lookup table. take corresponding ind value. assign result of lapply mydf (via mydf[]<-) keep data.frame structure of mydf.
hope clarifies little.
data
mydf<-structure(list(ap = structure(c(1l, 1l, 1l), .label = "a", class = "factor"), bp = structure(c(2l, na, 1l), .label = c("ag", "at"), class = "factor"), cp = structure(c(2l, 3l, 1l), .label = c("g", "ga", "gt"), class = "factor")), .names = c("ap", "bp", "cp"), class = "data.frame", row.names = c(na, -3l))
Comments
Post a Comment