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.
one way is: ind <- ave(Data$IA, Data$Sub, Data$Tr, FUN = function (x) !duplicated(x)) Data[as.logical(ind), ] I hope it helps. Best, Dimitris jwg20 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) > }-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
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.gullifer at 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 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.
>>
--
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. ~
pmatch() facilitates a very simple solution: #Data IA <- factor(c(1,2,2,3,3,4,3,5,5)) FixTime <- c(200,350,500,600,700,850,1200,1350,1500) #First occurrence of each level first. <- pmatch(levels(IA),IA) #Use first occurrence to subscript a vector or data frame FixTime[first.] A simple way to apply pmatch across unique combinations of levels of several factors is to create a new composite factor with paste() and factor(), then proceed as above. -- View this message in context: http://www.nabble.com/Calculating-First-Occurance-by-a-factor-tp22789964p22834451.html Sent from the R help mailing list archive at Nabble.com.