0

I have made a sham DF corresponding to smth I have to do for my research.

What I want to do is compare in a t.test col1 to col2, col3 to col4, etc.

THE DF:

Tibble: 19 x 10
    col1  col2  col3  col4  col5  col6  col7  col8  col9 col10
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1    11     2    23     4    55     1    45     9    57
 2     2    12     3    24     6    56     3    46    10    58
 3     3    13     4    25     8    57     5    47    11    59
 4     4    14     5    26    10    58     7    48    12    60
 5     5    15     6    27    12    59     9    49    13    61
 6     6    16     7    28    14    60    11    50    14    62
 7     7    17     8    29    16    61    13    51    15    63
 8     8    18     9    30    18    62    15    52    16    64
 9     9    19    10    31    20    63    17    53    17    65
10    10    20    11    32    22    64    19    54    18    66
11    11    21    12    33    24    65    21    55    19    67
12    12    22    13    34    26    66    23    56    20    68
13    13    23    14    35    28    67    25    57    21    69
14    14    24    15    36    30    68    27    58    22    70
15    15    25    16    37    32    69    29    59    23    71
16    16    26    17    38    34    70    31    60    24    72
17    17    27    18    39    36    71    33    61    25    73
18    18    28    19    40    38    72    35    62    26    74
19    19    29    20    41    40    73    37    63    27    75

I tried subsetting the 2 observations in separate DF and then wanted to use a nested "for" loop but I am not sure how to proceed.

tbl <- sham_data
subjA <- subset.data.frame(tbl, ,select = c(col1, col3, col5, col7, col9))
subjB <- subset.data.frame(tbl, ,select = c(col2, col4, col6, col8, col10))
for (col in subjA) {
   for(col in subjB){
   t.test(.............. DONT KNOW WHAT TO DO .................)
     }}

1 Answers1

0

You are almost there. Instead of splitting the data frame, you just need to create a grouping vector and loop through them:

sham_data <- data.frame(matrix(1:190,19,10))
# create groups
grps<-rep(1:5,each=2)
# 1 is column 1-2, 2 is column 3-4 and so on..
# use a list to store the results
results <- vector("list",5)
names(results) <- unique(grps)
for(i in unique(grps)){
results[[i]] <- t.test(sham_data[,grps==i][,1],sham_data[,grps==i][,2])
}
head(results)
# or use lapply
results <- lapply(unique(grps),function(i){
t.test(sham_data[,grps==i][,1],sham_data[,grps==i][,2])
})
StupidWolf
  • 45,075
  • 17
  • 40
  • 72