How to loop/iterate a script to extract data for each participant
20:59 24 May 2022

I have created a script to extract, manipulate, and reorganise the information for a single .csv file (which contains the outcome of an experiment for ONE participant). (i.e., each .csv file contains information for one participant)

Now I would like to iterate the same process for each .csv file (more than 200) that I have in the same folder (which contains the outcome of the experiment for each participant).

I have managed to ask R to read each .csv file in the folder like this:

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))

Now I need to apply the iteration/loop to this entire script (that for 1 participant is as follows):

(I WILL REPOST THE FULL SCRIPT BELOW)

In summary, the main thing I need is for the loop to run the same script for each Participant in this study (in this example, participant M075), I need it to do the same for M076, M077, etc, etc, etc.

#install tidyerse package
#install.packages("tidyverse")
#Load packages 
#library(tidyverse)

#Read .csv and transform to df
data1<- read.csv('M075_4.csv', header=FALSE,  stringsAsFactors = FALSE)

# Count number of correct trials (to be added later in final summary table).
Correct_responses <-sum(data1$V7 == 1) 

#Select only correct responses 
correct_data <- filter(data1,V7 == TRUE)  # the filter function belongs to package dplyr

#Remove extreme values (data trimming) to leave the final data for analysis (note V6 = reaction time)

final_data <- correct_data %>%
  filter(V6 > 0.200 & V6 < 2.00) %>% #first set of boundaries 
  mutate(cutoff_above = mean(V6) + 2*sd(V6)) %>% #create a cutoff for 2 SDs above the mean
  mutate(cutoff_below = mean(V6) - 2*sd(V6)) %>% #create a cutoff for 2 SDs below the mean
  filter(V6 < cutoff_above & V6 > cutoff_below ) %>% #remove extreme values above 3 SDs of the mean
  select(-cutoff_above) %>% #remove the cutoff column to tidy the table up 
  select(-cutoff_below) #remove the cutoff column to tidy the table up 

#Correct responses left after data trimming
Correct_and_timely_responses <-sum(final_data$V7 == 1)
 


write.csv(final_data, #final analysis ready table 
          file = " M075_final.csv", #file name to be saved as in your WD 
          row.names = FALSE) #avoid a new column from being created with row numbers.

#Get the mean RT data for each condition after combining the two condition columns (V3 and V8)
AB_data <- final_data %>% 
  mutate(Condition = paste0(V3, V8)) %>% #combine the two elements into separate conditions
  group_by(Condition) %>% #group the analysis to get separate means for each condition 
  summarise(MeanRT = mean(V6)) #get the mean RT for each condition

#transpose rows and columns
AB_data_t <- as.data.frame(t(AB_data)) 

# rename columns
colnames(AB_data_t)[1] <- "AvS_Incongruent"
colnames(AB_data_t)[2] <- "AvS_Congruent"
colnames(AB_data_t)[3] <- "AvC_Incongruent"
colnames(AB_data_t)[4] <- "AvC_Congruent"
colnames(AB_data_t)[5] <- "CvS_Incongruent"
colnames(AB_data_t)[6] <- "CvS_Congruent"

#define new columns to add
Correct_responses <- c(Correct_responses)
Correct_after_trimming <- c(Correct_and_timely_responses)

#add column called 'Correct'
AB_data_t$Correct_responses <- Correct_responses
AB_data_t$Correct_after_trimming <- Correct_after_trimming

#add column by Ps number (NOT SURE HOW TO DO THIS!!! in loop)

#remove first row
AB_data_t<-AB_data_t[-1,]

#remove first column
AB_data_t<-AB_data_t[,-1]

#save as RT data for future reference 
write.csv(AB_data_t, #final data
          file = "M075_Means.csv") #file name to be saved as in your WD
r loops csv iteration