library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
###############################
#FUNCTION: t_test
# purpose: to take the values of two different numbers, in our case it will be eye sizes, and perform a t-test
# input: a data file which will consist of 2 columns (left and right eyesize) and compare a t.test across them
# output: the p-value of this test
#------------------------------
t_test <- function(d=NULL)  {
  if(is.null(d)) {
     x_var<-rnorm(10)
     y_var<-rnorm(10)
     d<-data.frame(x_var,y_var)}
   
.<- t.test(data=d , d[,1] , d[,2], paired=TRUE)

.<-print(.$p.value)
return(.)
}
t_test()
## [1] 0.3885487
## [1] 0.3885487
#--------------------------------------------
# Global variables
file_folder <- "RandomFiles/"
file_out <- "StatsSummary.csv"
file_names <- list.files(path=file_folder)
#--------------------------------------------

ID<-seq_along(file_names)#column  with the file number
pval<-rep(NA)#column to be filled with NA
stats_out <- data.frame(ID, file_names, pval)# make data frame

print(stats_out)
##   ID   file_names pval
## 1  1  48 MO A.csv   NA
## 2  2  48 MO B.csv   NA
## 3  3 48 UIC A.csv   NA
## 4  4 48 UIC B.csv   NA
# batch process by looping through individual files
for (i in seq_along(file_names)) {
  data <- read.csv(file=paste(file_folder,file_names[i],sep=""),
                      sep=",",
                      header=TRUE) # read in next data file

 glimpse(data)
 summary(data)
 print(data)


d_clean <- data[complete.cases(data),] # get clean cases

.<-t_test(d_clean)
print(d_clean)
 stats_out[i,3]<-unlist(.) #this adds the p value and run to the dataframe under the section I created which was filled with NA
}
## Rows: 6
## Columns: 2
## $ Left.eye  <int> 312, 298, 251, 319, 318, 335
## $ Right.eye <int> 310, 303, 377, 330, 314, 328
##   Left.eye Right.eye
## 1      312       310
## 2      298       303
## 3      251       377
## 4      319       330
## 5      318       314
## 6      335       328
## [1] 0.3543283
##   Left.eye Right.eye
## 1      312       310
## 2      298       303
## 3      251       377
## 4      319       330
## 5      318       314
## 6      335       328
## Rows: 7
## Columns: 2
## $ Left.eye  <int> 411, 397, 454, 478, 419, 546, 455
## $ Right.eye <int> 303, 563, 464, 460, 482, 550, 517
##   Left.eye Right.eye
## 1      411       303
## 2      397       563
## 3      454       464
## 4      478       460
## 5      419       482
## 6      546       550
## 7      455       517
## [1] 0.4541286
##   Left.eye Right.eye
## 1      411       303
## 2      397       563
## 3      454       464
## 4      478       460
## 5      419       482
## 6      546       550
## 7      455       517
## Rows: 9
## Columns: 2
## $ Left.eye  <int> 510, 452, 578, 648, 438, 507, 557, NA, NA
## $ Right.eye <int> 512, 685, 455, 508, 650, 450, 650, 450, 648
##   Left.eye Right.eye
## 1      510       512
## 2      452       685
## 3      578       455
## 4      648       508
## 5      438       650
## 6      507       450
## 7      557       650
## 8       NA       450
## 9       NA       648
## [1] 0.6044909
##   Left.eye Right.eye
## 1      510       512
## 2      452       685
## 3      578       455
## 4      648       508
## 5      438       650
## 6      507       450
## 7      557       650
## Rows: 8
## Columns: 2
## $ Left.eye  <int> 569, 424, 402, 550, 553, 589, 432, 403
## $ Right.eye <int> 636, 506, 606, 573, 421, NA, NA, NA
##   Left.eye Right.eye
## 1      569       636
## 2      424       506
## 3      402       606
## 4      550       573
## 5      553       421
## 6      589        NA
## 7      432        NA
## 8      403        NA
## [1] 0.4192725
##   Left.eye Right.eye
## 1      569       636
## 2      424       506
## 3      402       606
## 4      550       573
## 5      553       421
#set up output file and incorporate time stamp and minimal metadata
   write.table(cat("# Summary stats for ",
                     "batch processing of regression models","\n",
                     "# timestamp: ",as.character(Sys.time()),"\n",
                     "# JAG","\n",
                     "# ------------------------", "\n",
                     "\n",
                     file=file_out,
                     row.names="",
                     col.names="",
                     sep=""))
## ""
# now add the data frame
   write.table(x=stats_out,
               file=file_out,
               row.names=FALSE,
               col.names=TRUE,
               sep=",",
               append=FALSE)

Alt text