[R] Calculating First Occurance by a factor

Glen Sargeant gsargeant at usgs.gov
Wed Apr 1 21:59:34 CEST 2009


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.




More information about the R-help mailing list