0

I have a dataset of 2 columns and 100 rows. I want to apply t-tests on every 10 rows of the 2colums, for example: test 1 for values[1:10] of columns A and B, test 2 for values [2:11] of columns A and B, test 3 for values[3:12] of columns A and B, etc, until test 91 for values [91: 100] of columns A and B. Now my way is like this for every test:

    RC.1 <- RC9[1:10]
    RM.1 <- RM9[1:10]
    easting.1 <- strip9$xcoord[1:10]
    northing.1 <- strip9$ycoord[1:10]
    coord1 <- data.frame(easting.1, northing.1)
    modified.p1 <- modified.ttest(RC.1, RM.1, coord1)

Anyone knowns an efficient way to do it, instead of typing 91 tests manually?

Thanks.

Summer
  • 3
  • 3
  • Welcome to SO! Can you please provide the code and data you are working with? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Harrison Jones Oct 29 '21 at 12:18

1 Answers1

0

You may try this approach with a for loop -

n <- 10
k <- NROW(strip9) - n + 1
result <- vector('list', k)

for(i in 1:k) {
  index <- i:(i+n-1)
  RC.1 <- RC9[index]
  RM.1 <- RM9[index]
  easting.1 <- strip9$xcoord[index]
  northing.1 <- strip9$ycoord[index]
  coord1 <- data.frame(easting.1, northing.1)
  result[[i]] <- modified.ttest(RC.1, RM.1, coord1)
}

result
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your answer, but it shows "Error in vector("list", k) : invalid 'length' argument". Also, could you please explain why k <- nrow(RC9) - n + 1 ? – Summer Oct 29 '21 at 13:38
  • I thought `RC9` is a dataframe. Can you try the updated answer. My goal here is to create `k <- 91` since that is the number of outputs you'll have but instead of hardcoding it to 91 I have calculated it dynamically based on your data. – Ronak Shah Oct 29 '21 at 13:42
  • It works now ! Thanks a lot! – Summer Oct 29 '21 at 14:22