[R] Loop through each subject

David Winsemius dwinsemius at comcast.net
Tue Jun 28 21:34:31 CEST 2011


On Jun 28, 2011, at 2:51 PM, Edward Patzelt wrote:

> Huh, not sure I understand your feedback.  There is only 1 file that  
> is 1
> large dataframe.  I want to execute the commands on each subject in  
> the
> dataframe and ouput their respective files with the subject # and  
> file type
> appended (10_green.txt).

You named that file, paste(dat$Subject[[1]] plus a constant string. I  
assumed you meant to name a new file for each subject. And it still  
appears you wanted that. Your code won't give it to you because you  
are not using an argument to paste that is local to the inner loop. dat 
$Subject[[1]] will always be the same result.

Try instead something like:   paste(i, "red.txt", sep="_")

Or perhaps:  paste(colr[1,"Subject"], "red.txt", sep="_") # since colr  
and colg will vary.

I also incorrectly parsed the paste function thinking that the  
append="red.txt" and "green.txt" were arguments to write.table's  
append argument. You can discard those comments from consideration.

Is a tab a legitimate character in your OS naming convention? Seems  
error-prone to me, but what do I know?

-- 
david


>
> How would I do this loop index?
> - Show quoted text -
>
> On Tue, Jun 28, 2011 at 1:27 PM, David Winsemius <dwinsemius at comcast.net 
> >wrote:
>
>>
>> On Jun 28, 2011, at 1:44 PM, Edward Patzelt wrote:
>>
>> R help -
>>>
>>> I am attempting to write a script that has multiple subjects in 1  
>>> data
>>> file.
>>> Each subject has multiple rows with columns as variables.  Here is  
>>> my
>>> code,
>>> I am having problem executing it on each unique subject id (dat 
>>> $Subject).
>>>
>>
>> One problem that I see is that you are calling all of your files  
>> the same
>> thing (i.e. overwriting earlier results. Why aren't you using the  
>> loop index
>> in the naming process?
>>
>> (And aeppnd is a logical argument in write.table.)
>>
>> ?write.table
>>
>> --
>> David.
>>
>>>
>>> getwd()
>>>
>>> setwd("/Users/edwardpatzelt/**Desktop/Neuroimaging/MERIT/**SRRT/ 
>>> merge")
>>>
>>>
>>> dat <- read.table("test2.txt", header = TRUE, na.strings = NA,
>>> stringsAsFactors = FALSE, sep = "\t")
>>>
>>>
>>> for(i in 1:length(dat))
>>>
>>> {
>>>
>>> for (i in 1:)dat[(unique(dat$Subject)),)**]
>>>
>>> {
>>>
>>>  colg <- dat[grep("Green", dat$CueProbe),]
>>>
>>> colg <- data.frame(colg$SRRTCue.**OnsetTime/1000,
>>> (colg$SRRTFix2.OnsetTime-
>>> colg$SRRTCue.OnsetTime)/1000, (ifelse((colg$SRRTProbe.ACC == 1 |
>>> colg$Probe==
>>> "+"), 1, 0)))
>>>
>>> colr <- dat[grep("Red", dat$CueProbe),]
>>>
>>> colr <- data.frame(colr$SRRTCue.**OnsetTime/1000,
>>> (colr$SRRTFix2.OnsetTime-
>>> colr$SRRTCue.OnsetTime)/1000, (ifelse((colr$SRRTProbe.ACC == 1 |
>>> colr$Probe==
>>> "+"), 1, 0)))
>>>
>>>   write.table(colg, file  = paste(dat$Subject[[1]], sep = "\t",  
>>> append =
>>> "green.txt"), col.names = FALSE, row.names = FALSE)
>>>
>>> write.table(colr, file  = paste(dat$Subject[[1]], sep = "\t",  
>>> append =
>>> "red.txt"), col.names = FALSE, row.names = FALSE)
>>>
>>>   }
>>>
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Edward H. Patzelt
>>> Research Assistant – TRiCAM Lab
>>> University of Minnesota – Psychology/Psychiatry
>>> VA Medical Center
>>> Office: S355 Elliot Hall - Twin Cities Campus
>>> Phone: 612-626-0072  Email: patze003 at umn.edu
>>>
>>> Please consider the environment before printing this email
>>> www.psych.umn.edu/research/**tricam<http://www.psych.umn.edu/research/tricam 
>>> >
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help 
>>> >
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>
>
> -- 
> Edward H. Patzelt
> Research Assistant – TRiCAM Lab
> University of Minnesota – Psychology/Psychiatry
> VA Medical Center
> Office: S355 Elliot Hall - Twin Cities Campus
> Phone: 612-626-0072  Email: patze003 at umn.edu
>
> Please consider the environment before printing this email
> www.psych.umn.edu/research/tricam
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list