1

Assume I have a dataset something like

df <- data.frame(dive=factor(sample(c("dive1","dive2"),10,replace=TRUE)),speed=runif(10))

Now my goal is to find " Total mean of the data" and "Mean by Subgroups in R" in same data. So, I can say I should get something like

#    dive  Total_Mean   speed
# 1 dive1   0.52        0.5790946
# 2 dive2   0.52        0.4864489

I am using a code

df%>% summarise(avg=mean(speed))%>%
group_by(dive)%>%
summarise(Avg_group=mean(dive))

Which is wrong I know, So all I am seeking is how can I group by and open my data gain in dplyr for performing different operations at different time

Manu Sharma
  • 1,593
  • 4
  • 25
  • 48

2 Answers2

4

Try this:

df %>% 
   mutate(avg=mean(speed)) %>% 
   group_by(dive) %>% 
   summarise(Avg_group=mean(speed),Total_Mean=first(avg))
nicola
  • 24,005
  • 3
  • 35
  • 56
  • Hi nikola, Thank you so much. It worked like a charm. 1 quick question, what if I had more than 3 group by columns in summarise, and I wanted Total average at two groups instead of whole data – Manu Sharma May 25 '17 at 08:27
  • 1
    Sorry, I am not sure I understand your question. Consider that you can certainly open a new question and state clearly (as you did in this question) what's your input and desired output. You'll very likely receive useful help. – nicola May 25 '17 at 08:34
1

We can use data.table

library(data.table)
setDT(df)[, .(Avg_group = mean(speed), Total_mean = mean(df$speed)),.(dive)]  
#     dive Avg_group Total_mean
#1: dive2 0.4733421  0.4238937
#2: dive1 0.3744452  0.4238937
akrun
  • 874,273
  • 37
  • 540
  • 662