I discovered Hadley Wickham's "plyr" package last week and have found it very useful in circumstances like this:
library(plyr) firstfixtime = ddply( .data = data , .variables = c('Sub','Tr','IA') , .fun <- function(df){ df$FixTime[which.min(df$FixInx)] } ) > On Mon, Mar 30, 2009 at 3:40 PM, jwg20 <jason.gulli...@gmail.com> wrote: >> >> I'm having difficulty finding a solution to my problem that without using a >> for loop. For the amount of data I (will) have, the for loop will probably >> be too slow. I tried searching around before posting and couldn't find >> anything, hopefully it's not embarrassingly easy. >> >> Consider the data.frame, Data, below >> >> Data >> Sub Tr IA FixInx FixTime >> p1 t1 1 1 200 >> p1 t1 2 2 350 >> p1 t1 2 3 500 >> p1 t1 3 4 600 >> p1 t1 3 5 700 >> p1 t1 4 6 850 >> p1 t1 3 7 1200 >> p1 t1 5 8 1350 >> p1 t1 5 9 1500 >> >> What I'm trying to do is for each unique IA get the first occurring FixTime. >> This will eventually need to be done by each Trial (Tr) and each Subject >> Number (Sub). FixInx is essentially the number of rows in a trial. The >> resulting data.frame is below. >> >> Sub Tr IA FirstFixTime >> p1 t1 1 200 >> p1 t1 2 350 >> p1 t1 3 600 >> p1 t1 4 850 >> p1 t1 5 1350 >> >> Here is the solution I have now. >> >> agg = aggregate(data$FixInx, list(data$Sub, data$Tr, data$IA), min) #get the >> minimum fix index by Sub, Tr, and IA... I can use this min fix index to pull >> out the desired fixtime >> >> agg$firstfixtime = 0 # new column for results >> >> for (rown in 1:length(rownames(agg))){ #cycle through rows and get each >> data$firstfixtime from FixTime in matching rows >> agg$firstfixtime[rown] = as.character(data[data$Tr == agg$Group.2[rown] & >> data$Sub == agg$Group.1[rown] & data$IA == agg$Group.3[rown] & data$FixInx >> == agg$x[rown], ]$FixTime) >> } >> -- >> View this message in context: >> http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22789964.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help@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. >> -- Mike Lawrence Graduate Student Department of Psychology Dalhousie University Looking to arrange a meeting? Check my public calendar: http://tinyurl.com/mikes-public-calendar ~ Certainty is folly... I think. ~ ______________________________________________ R-help@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.