I'll take this old post as reference. So, the modified dataset looks like the following:
df <- data.frame(dive = factor(sample(c("dive1","dive2","dive3","dive4"), 14, replace=TRUE)),
speed = runif(14)
)
> df
dive speed
1 dive1 0.627296799
2 dive1 0.288594538
3 dive4 0.598177856
4 dive2 0.371158436
5 dive2 0.827468739
6 dive3 0.485977449
7 dive2 0.151295215
8 dive4 0.773988372
9 dive2 0.567155356
10 dive1 0.008585884
11 dive4 0.433648371
12 dive2 0.759196515
13 dive2 0.641193241
14 dive3 0.089451537
I would like to modify the column speed
so that it contains the mean per group (same entry for each .group
) for dive1
and dive2
, and do nothing (keep df
as it is) for the other two groups).
I tried with if
(and, of course, group_by
and summarise
), but that's not what I want, I receive a warning message and only 4 results...
df2 <- if(!(df$dive %in% c("dive3", "dive4"))){
summarise(group_by(df, dive), speed = mean(speed))
}
Warning message:
In if (!(df$dive %in% c("dive3", "dive4"))) { :
the condition has length > 1 and only the first element will be used
> df2
# A tibble: 4 x 2
dive speed
<fct> <dbl>
1 dive1 0.860
2 dive2 0.460
3 dive3 0.277
4 dive4 0.330