-1

I am trying to calculate the median of specific groups using RStudio. I have inputted my data using a .csv file and my data set (MyData2) looks like the following:

 score group
1      7 DiseaseX
2     14 DiseaseX
3      6 DiseaseX
4      6 DiseaseX
5      5 DiseaseX
6      5 DiseaseX
7      5 DiseaseX
8     14 DiseaseX
9      9 DiseaseY
10     5 DiseaseY
11     9 DiseaseY
12     7 DiseaseY
13     6 DiseaseY
14     5 DiseaseY

How would I compute the mean 'score' for each Disease X group and the Disease Y group?

I have tried using the group_by function as follows but I keep receiving an error.

MyData2 %>% 
group_by(group) %>% 
summarize(median = median(MyData2),
sum = sum(MyData2))

Please note that I am a novice RStudio user and am trying to teach myself the basics of this program.

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Instead of ´median(MyData2)´, where you reference the whole dataframe, you need to reference the column, as in ´median(score)´. – coffeinjunky Jun 23 '20 at 17:55
  • replace `MyData2` with the name of the variable from which you want to calculate the median, so I guess in your example with `score` – Ahorn Jun 23 '20 at 17:56

1 Answers1

0

In Base-r we can use for example

lapply( split(MyData2$score,MyData2$group), mean)
Daniel O
  • 4,258
  • 6
  • 20