How to convert time (mm:ss) to decimal form in R

前端 未结 3 887
独厮守ぢ
独厮守ぢ 2020-11-30 13:52

I\'ve imported a csv-file to R using RStudio where I am trying to plot points per game against minutes per game. However the minutes per game is in the format mm:ss and I\'m

3条回答
  •  情书的邮戳
    2020-11-30 14:19

    Do you need to decimalise it? If you store the data in the correct format, for example as an object of class POSIXlt, one of R's date-time classes, R will handle the correct handling of the times in numeric fashion. Here is an example of what I mean:

    First we create some dummy data for illustration purposes:

    set.seed(1)
    DF <- data.frame(Times = seq(as.POSIXlt("10:00", format = "%M:%S"), 
                                 length = 100, by = 10),
                     Points = cumsum(rpois(100, lambda = 1)))
    head(DF)
    

    Ignore the fact that there are dates here, it is effectively ignored when we do the plot as all observations have the same date part. Next we plot this using R's formula interface:

    plot(Points ~ Times, data = DF, type = "o")
    

    Which produces this:

    points per game time

提交回复
热议问题