[R] Calculating First Occurance by a factor

jwg20 jason.gullifer at gmail.com
Mon Mar 30 20:40:08 CEST 2009


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.




More information about the R-help mailing list