Question: Average dataframe columns according to a corresponding vector.
1
gravatar for goldie.ed
5 days ago by
goldie.ed10
goldie.ed10 wrote:

I have gene expression data frame: df example

and i have a vector which correspond to the columns: vector

I would like to average the mean of the same columns according to the vector. so the two first two "Lm 10" columns will average together and so on...

what is the most efficient way to do that?

Thanks

R • 74 views
ADD COMMENTlink modified 5 days ago by ATpoint17k • written 5 days ago by goldie.ed10

Please paste your date as text, and provide expected output.

ADD REPLYlink written 5 days ago by zx87547.5k
1
gravatar for ATpoint
5 days ago by
ATpoint17k
Germany
ATpoint17k wrote:

Without knowing exactly how the data look like, it will come down to something like:

sapply(X = unique(your.vector), 
       FUN = function(x){
         tmp.grep <- grep(x, colnames(your.df))
         return( rowMeans(your.df[,tmp.grep]) )
       }
)
ADD COMMENTlink written 5 days ago by ATpoint17k

This wont work for me - the vector does not correspond to the column names.

ADD REPLYlink written 5 days ago by goldie.ed10

Then I ask you a third time now to give a proper example on how the vector and the dataframe look like so that one can reproduce the problem. This screenshot you provide is tiny and does not help.

ADD REPLYlink written 5 days ago by ATpoint17k

I was able to change the vector elements to represent a pattern within the column names and thus was able to use your suggested solution.

Still - I wonder if there is another way in situations when the elements does not share a similar patterns.

ADD REPLYlink written 5 days ago by goldie.ed10
0
gravatar for krushnach80
5 days ago by
krushnach80500
krushnach80500 wrote:
library(dplyr) 
    Final_average  <- dataframe %>% mutate(lm10 = rowMeans(.[grep("Lm", names(.))]), 
                                       SL10 = rowMeans(.[grep("SL", names(.))]),
                                       Control10= rowMeans(.[grep("Control",names(.))]))

This should work fine

ADD COMMENTlink written 5 days ago by krushnach80500
1

Now imagine vector has 1000 items in it.

ADD REPLYlink modified 5 days ago • written 5 days ago by zx87547.5k

Thank you very much the thing is - the number is the patient number - and the data contains about 40 patients. I want to average each of them.

just to be clear - patient has 3 conditions - Lm, SL , control. and i would like to average all his control samples together... and to do this for all patients.

ADD REPLYlink written 5 days ago by goldie.ed10
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1790 users visited in the last hour