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,918
  • 2
  • 20
  • 39
jkim
  • 81
  • 7

2 Answers2

4

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
  • 7,235
  • 2
  • 27
  • 38
3

You can use summarise_all:

library(dplyr)
data %>% group_by(gene) %>% summarise_all(funs(mean))
RLave
  • 8,144
  • 3
  • 21
  • 37