I have looked at many similar questions (like this one), but in my case the treatment groups are not saved as separate vectors, and I haven't had any success substituting my variable names into any other code I've seen on this topic.
I want to compare means for "before" and "after" treatments for the same variable (test score) across multiple locations.
My data looks like this:
> head(my.df, n=15)
Location TestScore Treatment
1 4 0.7167641 Before
2 4 0.7998261 Before
3 4 0.8165880 After
4 4 0.8078955 After
5 7 0.6993413 Before
6 7 0.8404255 Before
7 7 0.7803164 Before
8 7 0.8383867 After
9 7 0.7930419 After
10 8 0.8504963 Before
11 8 0.7734653 Before
12 8 0.8408432 After
13 8 0.7980454 After
14 8 0.8407756 After
15 8 0.7837427 After
Note that the number of "before" and "after" responses is different both within and between locations.
I know I can use the following code to compare the before and after treatments across ALL locations:
t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
However, I want to compare the before and after values for EACH location (since I have 100+ locations), not ALL locations at once. Ideally I could generate a list or table of p-values without having to write a new line of code each time. I thought I could do something simple like adding "group_by" like I've shown below:
my.df %>% group_by(Location) %>% do(tidy(t.test(TestScore ~ Treatment, data = my.df, var.equal = FALSE)
but when I run this code I get an output with the same p-value for every location (even though the data are different), as shown below:
# A tibble: 10 x 11
# Groups: Location [10]
Location estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 4 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
2 7 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
3 8 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
4 9 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
5 10 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
6 12 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
7 14 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
8 16 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
9 21 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
10 27 0.0587 0.972 0.913 15.0 1.60e-20 51.8 0.0508 0.0665 Welch Two Sample t-test two.sided
How can I get separate p-values comparing the before and after treatments for each location? Any help is greatly appreciated!