0

i have a data frame that contains lap times of the same race for the last 10 years (1 race a year from 2007 to 2017) i am trying to extract the fastest time of each race from each year. i thought that using min() would do the trick but it does not as it finds the smallest values within all the lap times from all the years. here is the code:

FL2007 = subset(AusLapTimes, AusLapTimes$Time.Seconds == min(AusLapTimes$Time.Seconds) & AusLapTimes$year == 2007)

2007 happens to be the year where the fastest lap of all time within the range occured so i do get data within the FL2007 DF but when i ran this:

FL2008 = subset(AusLapTimes, AusLapTimes$Time.Seconds == min(AusLapTimes$Time.Seconds) & AusLapTimes$year == 2008)

FL2009 = subset(AusLapTimes, AusLapTimes$Time.Seconds == min(AusLapTimes$Time.Seconds) & AusLapTimes$year == 2009)

the 2 extra DFs get created but with no values to display as the fasest ever lap was in 2007.

help please!

Raul Gonzales
  • 866
  • 1
  • 15
  • 28
  • Take your favorite method from the "mean by group" R-FAQ, and replace `mean` with `min`. – Gregor Thomas Jan 18 '18 at 22:28
  • For example, with `library(dplyr)`: `AusLapTimes %>% group_by(year) %>% summarize(min_time = min(Time.Seconds))`, or in base R: `aggregate(Time.Seconds ~ year, AusLapTimes, min)`. – Gregor Thomas Jan 18 '18 at 22:29
  • @Gregor thanks for the answer mate. i did not look properly in SO hence it ended up being a duplicate! – Raul Gonzales Jan 18 '18 at 22:34

0 Answers0