This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Average dataframe columns according to a corresponding vector.

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

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

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.

Please use ADD COMMENT and follow the suggestion from zx8754 to add a proper data example. This screenshot is barely readable.

2 answers

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]) )
       }
)

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

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.

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.

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

Now imagine vector has 1000 items in it.

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.

Log in to answer this question.