0

I have a dataframe name R_alltemp in R with 6 columns, 2 groups of data with 3 replicates each. I'm trying to perform a t-test for each row between the first three values and the last three and use apply() so it can go through all the rows with one line. Here is the code im using so far.

R_alltemp$p.value<-apply(R_all3,1, function (x) t.test(x(R_alltemp[,1:3]), x(R_alltemp[,4:6]))$p.value)

and here is a snapshot of the table

    R1.HCC827  R2.HCC827  R3.HCC827 R1.nci.h1975 R2.nci.h1975 R3.nci.h1975  p.value
1  13.587632  22.225083  15.074230    58.187465           79    82.287573 0.4391160
2   2.717526   1.778007   1.773439     1.763257            2     1.679338 0.4186339
3 203.814478 191.135711 232.320487   253.908939          263   263.656100 0.4904493
4  44.386264  45.339169  54.089884     3.526513            3     5.877684 0.3095634

it functions, but the p-values im getting just from eyeballing it seem wrong. For instance in the first line, the average of the first group is way lower than the second group, but my p value is only .4.

I feel like I'm missing something very obvious here, but I've been struggling with it for much longer than I'd like. Any help would be appreciated.

Ryan D
  • 105
  • 1
  • 1
  • 7
  • When asking for help, you should include a [simple reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example with sample input and desired output that can be used to test and verify possible solutions. Are you trying to use dplyr::filter or stats::filter or base::Filter? What do you have loaded? – MrFlick Apr 17 '18 at 00:57
  • This is a study design that needs something different than t-tests. You need statistical advice. (After you have a sensible plan, you should study basic R coding and work through basic examples to learn that parentheses are for function calls, not for indexing.) – IRTFM Apr 17 '18 at 07:36

2 Answers2

2

Your code is incorrect. I actually don't understand why it does not return an error. This part in particular: x(R_alltemp[,1:3]) should be x[1:3].

This should be your code:

R_alltemp$p.value2 <- apply(R_alltemp, 1, function(x) t.test(x[1:3], x[4:6])$p.value)

   R1.HCC827  R2.HCC827  R3.HCC827 R1.nci.h1975 R2.nci.h1975 R3.nci.h1975   p.value    p.value2
1  13.587632  22.225083  15.074230    58.187465           79    82.287573 0.4391160 0.010595829
2   2.717526   1.778007   1.773439     1.763257            2     1.679338 0.4186339 0.477533387
3 203.814478 191.135711 232.320487   253.908939          263   263.656100 0.4904493 0.044883436
4  44.386264  45.339169  54.089884     3.526513            3     5.877684 0.3095634 0.002853154

Remember that by specifying 1 it you are telling apply to get the columns. So function(x) returns the equivalent of this: x <- c(13.587632, 22.225083, 15.074230, 58.187465, 79, 82.287573) which means you want to subset the first three values by x[1:3] and then the last three x[4:6] and apply t.test to them.

A good idea before using apply is to test the function manually so if you do get odd results like these you know something went wrong with your code.

Amar
  • 1,340
  • 1
  • 8
  • 20
1

So the two-tailed p-value for the first row should be:

> g1 <- c(13.587632,  22.225083,  15.074230)
> g2 <- c(58.187465, 79, 82.287573)
> t.test(g1,g2)$p.value
[1] 0.01059583

Applying the function across all rows (I tacked the new p-val at the end as pval:

> tt$pval <- apply(tt,1,function(x) t.test(x[1:3],x[4:6])$p.value)
> tt
   R1.HCC827  R2.HCC827  R3.HCC827 R1.nci.h1975 R2.nci.h1975 R3.nci.h1975   p.value        pval
1  13.587632  22.225083  15.074230    58.187465           79    82.287573 0.4391160 0.010595829
2   2.717526   1.778007   1.773439     1.763257            2     1.679338 0.4186339 0.477533387
3 203.814478 191.135711 232.320487   253.908939          263   263.656100 0.4904493 0.044883436
4  44.386264  45.339169  54.089884     3.526513            3     5.877684 0.3095634 0.002853154

Maybe it's the double-use of the data frame name in the function (that you don't need)?

mysteRious
  • 4,102
  • 2
  • 16
  • 36