Skip to content

Calculating First Occurance by a factor

1 message · Glen A Sargeant

#
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.