0

Hello everyone :) I'm quite new to R so having quite a few difficulties atm but after trying to do a t-test I'm getting this error:

Error in match.arg(alternative) : 'arg' must be NULL or a character vector

This is the code I've used:

data %>%
  t.test(mean_resp_rew, condition, data = data)
data

and I'm very unsure as to what is going wrong.

Edit (not sure if this is the right way of giving reproducible data haha) the df is called data:

data sample: df sample

data_summary: data_summary

STRhythm
  • 113
  • 8
  • 3
    Please use a small reproducible example. I guess you need `data = .` – akrun Mar 08 '22 at 15:35
  • If you need additional help, please add sample data to your question. Just a small example to illustrate the problem. `dput()` is a nice command for generating a copy/pasteable example of an R object, e.g., `dput(df[1:10, c("relevant", "columns")])` for the first 10 rows of the relevant columns of `df`. – Gregor Thomas Mar 08 '22 at 15:51
  • Yeah, pictures of data are not nice to work with. That's why we ask for `dput()` so it is copy/pasteable. `t.test` expects raw data, not already-computed means and variances. And it expects two samples. You seem to have 4 categories? – Gregor Thomas Mar 08 '22 at 16:41
  • Here's [the FAQ on creating reproducible examples](https://stackoverflow.com/q/5963269/903061). Seeing your data it looks like the relevant columns are `condition` and `response`, so `dput(df[1:10, c("condition", "response")])` should work for the first 10 rows. But you should also refine what you want - a t.test is a test of 2 means, not 4. Maybe a linear model or an anova would be appropriate - but what sort of test to do is a question for stats.stackexchange, not stack overflow. – Gregor Thomas Mar 08 '22 at 16:43

1 Answers1

1

The pipe x %>% foo(y) is interpreted as foo(x, y). So your code is interpreted as t.test(data, mean_resp_rew, condition, data = data), with the data argument first and last. Looking at the ?t.test help page, if you use a data argument it expects a formula, so we could try this:

df %>% t.test(mean_resp_rew ~ condition, data = .)

Which should work assuming mean_resp_rew and condition are columns in your df data frame (and that condition has 2 unique values). There's no nesting, so we don't gain anything from the pipe in this case, so I would recommend keeping it simple with

t.test(mean_resp_rew ~ condition, data = df)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • hiya! thank you so much for your response :) I'm not sure if I'll be able to explain this correctly but mean_resp_rew is a product of a calculation earlier in the code and is stored under data_summary, so whenever i try to use it in the t-test it's telling me mean_resp_rew isn't found (which is why I initially put the pipeline as it was the only thing that worked) I've tried doing data = summary data which also doesn't work so I'm a bit stuck :( – STRhythm Mar 08 '22 at 15:44
  • You need to have access to both `mean_resp_rew` and `condition`. And they need to be the same length. If they are in different data frames you could do `t.test(data_summary$mean_resp_rew, df$condition)`, but in that case `data_summary` and `df` must have the same number of rows. If you need more help, please add sample data to your question to make it reproducible. – Gregor Thomas Mar 08 '22 at 15:50
  • uhmm i added screenshots of the sample although I'm not sure if thats helpful or not :( the two are unfortunately not the right lengths - tried various combinations of ```data_summary$mean_resp_rew ~ data_summary$condition, data = data``` but no success – STRhythm Mar 08 '22 at 16:26
  • Yeah, pictures of data are not nice to work with. That's why we ask for `dput()` so it is copy/pasteable. `t.test` expects raw data, not already-computed means and variances. And it expects two samples. You seem to 4 categories? – Gregor Thomas Mar 08 '22 at 16:41
  • hiya! thank you so much for your help and time earlier - turns out we were all over complicating it and were supposed to use the original ```resp_per_rew``` variable instead of the mean one which heavily complicated things, all sorted now :) – STRhythm Mar 08 '22 at 23:32