0

I have this dataframe:

enter image description here

I'm performing a t_test using this lapply approach:

columns = colnames(my_data)[-1]
my_t_test<-lapply(my_data[columns], function(x) t.test(x~my_data$Treatment,alternative='less'))

But it seems the t_test take x=my_data$Control and y=my_data$Stress, making the results a non-sense. As I'm testing the alternative hypothesis that the difference in the mean is less in the $Stress group, I want that the x argument will be my_data$Stress. And option is to change to alternative='greater', but is an awful approach, the other option is to change the order of the groups in my dataframe, but I´m looking for a programmatic solution.

any suggestion?

David López
  • 500
  • 5
  • 21
  • Do you have `factor` column. Here, you are using the formula method. It may be better to do `function(x) t.test(val ~ Treatment, data = data.frame(val = x, Treatment = my_data$Treatment))` – akrun Jun 28 '20 at 04:01
  • I think you are confused. If the is the way that your dataframe is displayed in an R console session then the column names, ie. the variable names are ‘21’, ‘28’, ‘35’, ... – IRTFM Jun 28 '20 at 04:08
  • @akrun,is not working, by the way, I changed the order of my dataframe putting the Stress rows first, but the result is the same. I'm confused – David López Jun 28 '20 at 04:23
  • @DavidLópez can you please use `dput` of the example to test thanks – akrun Jun 28 '20 at 04:24

1 Answers1

0

We can use the formula method after subsetting the columns of 'my_data' by looping over the column name vector

lapply(columns, function(nm) t.test(.~ Treatment, my_data[c('Treatment', nm)]))
akrun
  • 874,273
  • 37
  • 540
  • 662