In R, how to take the mean of a varying number of elements for each row in a data frame? -


so have dataframe, pvalues, this:

pvals <- read.csv(textconnection("pval1 pval2 pval3 0.1 0.04 0.02 0.9 0.001 0.98 0.03 0.02 0.01"),sep = " ") 

that corresponds dataframe, data, this:

 data <- read.csv(textconnection("col1 col2 co3  10 2 9  11 20 200  2 3 5"),sep=" ") 

for every row in data, i'd take mean of numbers indices correspond entries in pvalues <= 0.05.

so, example, first row in pvalues has 2 entries <= 0.05, entries in [1,2] , [1,3]. therefore, first row of data, want take mean of 2 , 9.

in second row of pvalues, entry [2,2] <=0.05, instead of taking mean second row of data, use data[20,20].

so, output like:

means 6.5 20 3.33 

i thought might able generate indices every entry in pvalues <=0.05, , use select entries in data use mean. tried use command generate indices:

exp <- which(pvalues[,]<=0.05, arr.ind=true) 

...but picks on indices entries first column <=0.05. in example above, output [3,1].

can see i'm doing wrong, or have ideas on how tackle problem?

thank you!

it's bit funny looking, should work

rowmeans(`is.na<-`(data,pvalues>=.05), na.rm=t) 

the "ugly" part calling is.na<- without doing automatic replacement, here set data p-values larger .05 missing , take row means.

it's unclear me doing exp, type of method work well. maybe with

expx <- which(pvalues[,]<=0.05, arr.ind=true)     aggregate(val~row, cbind(expx,val=data[exp]), mean) 

(renamed not interfere built in exp() function)

tested with

pvalues<-read.table(text="pval1 pval2 pval3 0.1  0.04 0.02 0.9  0.001 0.98 0.03 0.02 0.01", header=t)  data<-read.table(text="col1 col2 co3  10   2    9  11   20   200  2    3    5", header=t) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -