0

I am scratching my head over this, ive created a new column and would like to filter the J.D column based on that new column.

df %>%
filter(Year >= 1920, Year < 2019) %>%
select(Year,J.D) %>%
mutate(cutoff = ifelse(Year < 1970,1,0)) %>%
t.test(x = df[cutoff == 0][J.D], y = df[cutoff == 1][J.D] )
TheAnalyst
  • 33
  • 1
  • 8
  • 2
    Can you make this a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? I have a feeling the way you're trying to get the J.D column won't work but can't test that without your data. But you can think of how you would normally construct a `t.test` call—there's the argument `data`, which you're piping data into. Or, maybe more sustainably, filter, select, etc what you want to do the test on and save that to a variable first, then use that as the data for `t.test`, unless there's some reason why you *have* to pipe it in – camille Dec 07 '19 at 20:20
  • 2
    Why not use the formula interface to `t.test`? Something like `t.test(J.D ~ cutoff, data = .)` – Rui Barradas Dec 07 '19 at 20:21
  • Thanks used both of your suggestions and it works as intended. – TheAnalyst Dec 07 '19 at 20:30
  • You can also do t-tests with the {infer} package: https://infer.netlify.com/articles/two_sample_t.html – David Ranzolin Dec 07 '19 at 20:36

1 Answers1

3

An option is to use wrap within {} and use .$ to subset if we specify the 'x', 'y' option in t.test

df %>%
 filter(Year >= 1920, Year < 2019) %>%
 select(Year,J.D) %>%
 mutate(cutoff = (Year < 1970)) %>%
 {t.test(x = .$J.D[!.$cutoff], y = .$J.D[.$cutoff])}

Using a reproducible examplee

mtcars %>%
     {t.test(x = .$mpg[.$carb == 4], y = .$mpg[.$carb == 2])}
akrun
  • 874,273
  • 37
  • 540
  • 662