I'm having trouble using dplyr group_by to do a grouped mean and not sure what is going on.
When I create this reprex to perform a grouped mean it works exactly as usual and as I want it to:
library(tidyverse)
set.seed(2095)
df <- tibble(prot_id = LETTERS[1:10],
S1 = rnorm(10,30,1),
S2 = rnorm(10,30,1),
S3 = rnorm(10,30,1))
df
#> # A tibble: 10 x 4
#> prot_id S1 S2 S3
#> <chr> <dbl> <dbl> <dbl>
#> 1 A 29.5 28.9 30.6
#> 2 B 29.5 32.4 30.3
#> 3 C 31.4 30.5 30.3
#> 4 D 30.3 28.4 30.9
#> 5 E 29.6 29.7 29.1
#> 6 F 30.5 30.9 29.0
#> 7 G 28.8 31.7 29.5
#> 8 H 28.6 30.3 30.8
#> 9 I 28.8 30.0 30.1
#> 10 J 30.3 29.5 29.0
df %>% group_by(prot_id) %>%
mutate(mean_s = mean(c(S1,S2,S3)))
#> # A tibble: 10 x 5
#> # Groups: prot_id [10]
#> prot_id S1 S2 S3 mean_s
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 A 29.5 28.9 30.6 29.7
#> 2 B 29.5 32.4 30.3 30.7
#> 3 C 31.4 30.5 30.3 30.8
#> 4 D 30.3 28.4 30.9 29.9
#> 5 E 29.6 29.7 29.1 29.5
#> 6 F 30.5 30.9 29.0 30.2
#> 7 G 28.8 31.7 29.5 30.0
#> 8 H 28.6 30.3 30.8 29.9
#> 9 I 28.8 30.0 30.1 29.6
#> 10 J 30.3 29.5 29.0 29.6
mean(c(df$S1[1],df$S2[1],df$S3[1]))
#> [1] 29.66834
mean(c(df$S1[2],df$S2[2],df$S3[2]))
#> [1] 30.72519
Created on 2018-08-30 by the reprex package (v0.2.0).
However, when I run exactly the same code in RStudio I get this:
> df %>% group_by(prot_id) %>%
+ mutate(mean_s = mean(c(S1,S2,S3)))
# A tibble: 10 x 5
# Groups: prot_id [10]
prot_id S1 S2 S3 mean_s
<chr> <dbl> <dbl> <dbl> <dbl>
1 A 29.5 28.9 30.6 30.0
2 B 29.5 32.4 30.3 30.0
3 C 31.4 30.5 30.3 30.0
4 D 30.3 28.4 30.9 30.0
5 E 29.6 29.7 29.1 30.0
6 F 30.5 30.9 29.0 30.0
7 G 28.8 31.7 29.5 30.0
8 H 28.6 30.3 30.8 30.0
9 I 28.8 30.0 30.1 30.0
10 J 30.3 29.5 29.0 30.0
I've done this many times before, but today it's not working. Does anyone have any ideas what I'm doing wrong?