[R] Loop through each subject

David Winsemius dwinsemius at comcast.net
Tue Jun 28 20:27:43 CEST 2011


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
>
> 	[[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