Forgive me if my phrasing of this question is not ideal. I am new to stack overflow and a novice to R. I am working with a bunch of .txt files – one for each participant. Each file is named with the subject number (e.g., “015.txt”) and contains columns X, Y, and Time. I have code for calculating and creating columns of data for lead scores, difference scores, and rate of change one file at a time and then resaving that file (e.g., as “015LDscores.txt”).
setwd("/Users") #Set directory
my.data<-read.table(file.choose(),header=T,sep="\t")
#Lead scores for X and Y
require(DataCombine)
LeadX<-slide(my.data, Var="X", slideBy=-1)
LeadXY<-slide(LeadX, Var="Y", slideBy=-1)
LeadXY<-na.omit(LeadY) #Delete the first row of null lead values
#Difference scores for X and Y
LeadXY$DiffX<-(LeadXY$"X-1"-LeadY$"X")
LeadXY$DiffY<-(LeadXY$"Y-1"-LeadY$"Y")
#Rate of Change for X
LeadXY<-slide(LeadXY, Var="X", slideBy=1) #Create column of lagged X scores (X1)
write.table(LeadXY,"/Users/mstoehr/015LDscores.txt",sep="\t") #Save
LeadXY$velocityX<-(LeadXY$"X1"-LeadY$"X-1")/2 #Calculate rate of change of X
write.table(LeadY,"/Users/mstoehr/015LDscores.txt",sep="\t") #Save
I want to integrate a for loop that tells R to do this for all the text files (saved according to the pattern “*.txt”) in a specified directory. Here’s some separate code for a for loop.
setwd("/Users")
subjectFiles <- list.files()
for(i in 1:length(subjectFiles)){
#subject calculations}
Unfortunately, I am unsure of how to integrate these and have failed every attempt to integrate the for loop with just some of the above commands.