0

Working on my own side project, I have a smaller dataframe containing only 2 columns that was subsetted from a larger dataframe.

The 2 columns I am working on is "Lap.." and "Timestamp..s.".

I want to get the minimum timestamp and maximum timestamp from a specific lap number.

Right now my focus is having a hard coded value for the specific lap number.

Here is my code:

time_df <- csv_to_Table[c("Lap..", "Timestamp..s." )]
#output data to csv to make sure that it is correct
write.csv(time_df, file = "data/lap_timestamp.csv")
output$time_test <- renderText({
  max(time_df$Timestamp..s.) - min(time_df$Timestamp..s.)
})

The above code will display the total time that I was driving on the track.

However, when I read the max and min documentation it doesn't mention about having an extra criteria to filter on. Seeing dplyr library does contain a filter, I gave it a try, but still no luck.

output$time_test <- renderText({
  (max(time_df$Timestamp..s.) %>% filter(time_df$Lap.. == 1)) - (min(time_df$Timestamp..s.) %>% filter(time_df$Lap.. == 1))
})

Test data is located here: https://pastebin.com/GZvWEcXb

In the future I will want to move to having a dropdown for the lap number.

Any help/hint is appreciated.

blitz
  • 149
  • 1
  • 12
  • please could make life easier for persons wanting to help you by providing good example-code: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on that question. if you want to use dplyr you could use group_by and summarise: `time_df %>% group_by(Lap) %>% summarise(maxT=max(Timestamp..s..), minT=min(Timestamp..s.) %>% mutate (diff=maxT-minT)` – Jan Jun 26 '17 at 04:24

1 Answers1

0

You could also use aggregate(). If I understood you corectly your data looks like the following:

# Make up some data
set.seed(1)
df = data.frame(Lap = sample(1:10, size = 20, replace = TRUE),
                Timestamp = sample(seq.POSIXt(from = ISOdate(2017,1,1), to = ISOdate(2017,06,1), by = "day"), size = 20, replace = TRUE))

Then use aggregate() to get the min or max of each lap number:

aggregate(Timestamp ~ Lap, data = df, FUN = min)
aggregate(Timestamp ~ Lap, data = df, FUN = max)

Output:

  > aggregate(Timestamp ~ Lap, data = df, FUN = min)
   Lap           Timestamp
1    1 2017-02-21 13:00:00
2    2 2017-04-02 14:00:00
3    3 2017-02-10 13:00:00
4    4 2017-01-29 13:00:00
5    5 2017-04-12 14:00:00
6    6 2017-04-10 14:00:00
7    7 2017-02-28 13:00:00
8    8 2017-03-04 13:00:00
9    9 2017-02-28 13:00:00
10  10 2017-01-03 13:00:00

 > aggregate(Timestamp ~ Lap, data = df, FUN = max)
   Lap           Timestamp
1    1 2017-02-21 13:00:00
2    2 2017-04-02 14:00:00
3    3 2017-05-23 14:00:00
4    4 2017-04-21 14:00:00
5    5 2017-04-12 14:00:00
6    6 2017-04-10 14:00:00
7    7 2017-05-13 14:00:00
8    8 2017-05-06 14:00:00
9    9 2017-02-28 13:00:00
10  10 2017-01-20 13:00:00
FAMG
  • 395
  • 1
  • 9