0

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.

M21
  • 3
  • 2

1 Answers1

0

You can use list.files to only pull files with that pattern and then extract the patient number for use in saving the results. Something like this should work, assuming the text files are in the working directory and you want to pull all .txt files in that directory

subjectFiles <- list.files(pattern = '*.txt')
for (this_file in subjectFiles) {
  ID <- gsub('.txt', '', this_file)

  my.data <- read.table(this_file, 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, paste0("/Users/mstoehr/", ID, "LDscores.txt"),sep="\t") #Save
  LeadXY$velocityX<-(LeadXY$"X1"-LeadY$"X-1")/2 #Calculate rate of change of X 
  write.table(LeadY, paste0("/Users/mstoehr/", ID, "LDscores.txt"),sep="\t") #Save

}
MeetMrMet
  • 1,349
  • 8
  • 14
  • Ok, thanks. I tried that code and it does pull up all the text files in the directory, but it is not saving the new variable columns and I get the error message "Error: unexpected '}' in: " }" – M21 Nov 04 '16 at 17:22
  • Oops, left out a ) in the last `paste` call. Try that – MeetMrMet Nov 04 '16 at 17:23
  • That seemed to work. Thanks Craig! – M21 Nov 05 '16 at 23:41