0

I have a "tidy" dataframe and am trying to run a series of t-tests. The first column includes a series of food names. The next 3 columns include 0 or 1 values that specify the experimental condition. The last column includes the mean of the ratings of the particular food.

FoodName  Var1  Var2  Var3  Mean
hotdog    0     1     0     4.5
burger    1     1     0     5.5
fries     0     0     1     4.0
soda      1     0     1     6.0

I would like to run t.tests comparing the mean values for different groups depending on the conditions. Here are some examples of the types of t.tests I'd like to run:

  • comparing mean food score for all foods for which var1=0 to the mean food score for all foods for which var1=1
  • comparing mean foods score for all foods for which var1=0 and var2=1 to those for which var1=1 and var2=0

Here's some pseudocode for the examples:

t.test(mean if var1=0, mean if var1=1)
t.test(mean if var1=0 and var2=1, mean if var1=1 and var2=0)

How do I do this in R?

neilfws
  • 32,751
  • 5
  • 50
  • 63
melbez
  • 960
  • 1
  • 13
  • 36
  • 3
    The first is just `t.test(Mean ~ Var1, df)` (plus adjusting any other parameters you like). The second requires making a variable that represents what you want to test and then filter superfluous rows, either beforehand (e.g. with `mutate` and `filter`) or in context: `t.test(Mean ~ Var1, df, subset = Var1 != Var2)` (though you'll need more observations for it to actually run). – alistaire Feb 14 '18 at 04:57
  • 1
    Be sure to take into account the [multiple comparisons problem](https://en.wikipedia.org/wiki/Multiple_comparisons_problem) and employ an appropriate correction (such as Bonferroni). – LAP Feb 14 '18 at 08:01

0 Answers0