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

前端 未结 3 886
独厮守ぢ
独厮守ぢ 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:05

    Some tuning of first solution:

    minPerGame <- paste(sample(1:89,100000,T),sample(0:59,100000,T),sep=":")
    
    f1 <- function(){
    sapply(strsplit(minPerGame,":"),
     function(x) {
      x <- as.numeric(x)
      x[1]+x[2]/60
     }
    )
    }
    #
    f2<- function(){
     w <- matrix(c(1,1/60),ncol=1)
     as.vector(matrix(as.numeric(unlist(strsplit(minPerGame,":"))),ncol=2,byrow=TRUE)%*%w)
    }
    
    system.time(f1())
    system.time(f2())
    

    system.time(f1()) user system elapsed 0.88 0.00 0.86

    system.time(f2()) user system elapsed 0.25 0.00 0.27

提交回复
热议问题