R: pass function with multiple parameters to data frame rows, using only some columns -
what trying seems super simple, cannot around it, , i've looked around similar questions, still cannot solve it... it's stupid question, here goes...
i have 1 column lists positions , 2 other list start , end, need check whether position within range defined start , end, each row...
mwe:
within.range <- function(pos, start, end){ if (pos>=start & pos<=end){ return(true) } else{ return(false) } } my.df <- data.frame(gene=c("a","b","c","d","e"), chr=c(1,2,3,4,5), pos=as.numeric(c(34,23,6,46,765)), start=as.numeric(c(45,15,2,32,765)), end=as.numeric(c(86,38,9,41,767))) my.df
how can pass function data frame?? best attempt is:
apply(my.df[,c("pos","start","end")], 1, within.range, start=my.df$start, end=my.df$end)
but incorrect... maybe there whole better way accomplish same... thanks!
no need in apply
loops here, do
with(my.df, start <= pos & end >= pos) ## [1] false true true false true
if want add column, use transform
transform(my.df, check.pos = start <= pos & end >= pos) # gene chr pos start end check.pos # 1 1 34 45 86 false # 2 b 2 23 15 38 true # 3 c 3 6 2 9 true # 4 d 4 46 32 41 false # 5 e 5 765 765 767 true
Comments
Post a Comment