Search code examples
rdataframecomparep-value

Selecting rows and columns within .csv file automatically using r


So I have created a database for my thesis and I want to compare substances in different situations to receive a P-value. In order to do this I have created a script in R which is sort of not automatically. I am very interested in how I can create a script which does this for my using a for loop. I want to select groups for every line but also every three columns.

enter image description here

Please let me know if this is possible. Thanks in advance!

Right now I am doing it like this. First select line 1 first three values and then select next three values on first line in order to compare them in the next step.

row1_1 <- my_data[1, 2:4]
row1_2 <- my_data[1, 6:8]

Solution

  • The following will use a for loop to go through every row, as per the question

    for (i in 1:nrow(my_data))
    {
    
    case1<- my_data[i, 2:4]
    case2<- my_data[i, 6:8]
    compare_cases <- t.test(case1, case2, alternative = "two.sided", var.equal = FALSE) 
    print(case1)
    print(case2)
    print(compare_cases)
    }
    

    Modified variation with error handling, based on the comments

    pvalues <- c()
    for (i in 1:nrow(my_data))
    {
    
      case1<- my_data[i, 2:4]
      case2<- my_data[i, 6:8]
      pval <- NA
      test <- tryCatch({ t.test(case1, case2, alternative = "two.sided", var.equal = FALSE) }, 
                         warning = function(war) { NA },
                         error = function(err) { NA },
                         finally = { })
      print(paste0("Test for row i=",i))
      print(test)
      if (identical(test,NA)) pval <- NA
      else pval <- test$p.value
    
      pvalues <- c(pvalues,pval)
    }
    
    print("All my pvalues for all rows are:")
    print(pvalues)