Average dataframe columns according to a corresponding vector.
2
1
Entering edit mode
4.9 years ago
goldie.ed ▴ 30

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 • 1.0k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
4.9 years ago
ATpoint 82k

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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode
4.9 years ago
1769mkc ★ 1.2k
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 COMMENT
1
Entering edit mode

Now imagine vector has 1000 items in it.

ADD REPLY
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 1668 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6