Question: Average dataframe columns according to a corresponding vector.
1
goldie.ed10 wrote:

I have gene expression data frame: and i have a vector which correspond to the columns: 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 • 367 views
modified 17 months ago by ATpoint42k • written 17 months ago by goldie.ed10

1
ATpoint42k 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]) )
}
)
``````

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.

0
krushnach80850 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

1

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.