2

This is my data. What I would like to do is, if the gene column has duplicated value (e.g. CASZ1), then I would like to get mean values for each Sample column.

Input data

enter image description here

Output data

enter image description here

I googled it and tried, but I am stuck to get an answer. I am sorry for asking such a question looks exactly like homework.

My code

data %>% group_by(gene) %>% summarise(avg = mean(colnames(data)) --- error...
Terru_theTerror
  • 4,788
  • 2
  • 17
  • 37
jkim
  • 83
  • 7

2 Answers2

3

You can use summarise_all:

library(dplyr)
data %>% group_by(gene) %>% summarise_all(funs(mean))
RLave
  • 7,816
  • 3
  • 20
  • 35
3

You can use summarize_at along with some regular expression to ensure any column not starting by your pattern will not be included:

data %>% group_by(gene) %>% summarise_at(vars(matches("Sample")), mean)

Is that what you're looking for?

Vincent Bonhomme
  • 6,837
  • 2
  • 23
  • 35