r - converting hours:minutes with unevern column lengths - zeros -
i trying convert data.frame amount of time in format hours:minutes.
i found post useful , simple code approach of using posixlt field type.
r: convert hours:minutes:seconds
however each column represents month's worth of days. columns uneven. when try code below following several other posts, zeros in 1 column fewer row values.
the code below. note when run, zeros feb has fewer data values in rows.
rdf <- data.frame(jan=c("9:59","10:02","10:04"), feb=c("9:59","10:02",""), mar=c("9:59","10:02","10:04"),stringsasfactors = false) (i in 1:3) { res <- as.posixlt(paste(sys.date(), rdf[,i])) rdf[,i] <- res$hour + res$min/60 }
thank suggestions fix issue. i'm open more efficient approach well. best, leah
you try using package lubridate
. here converting data row row hour-minute format (using hm
), extracting hours, , adding minutes divided 60:
library(lubridate) rdf[] <- lapply(rdf, function(x){hm(x)$hour + hm(x)$minute/60}) jan feb mar 1 9.983333 9.983333 9.983333 2 10.033333 10.033333 10.033333 3 10.066667 na 10.066667
Comments
Post a Comment