hours = function(timestamps){ # converts timestamps into hour of the day. days = timestamps %>% unlist %>% as.POSIXlt(origin = "1970-01-01") %>% as.character %>% map(function(x){if(is.na(x)){return(c("NA","NA"))}else{return(strsplit(x, " "))}}) %>% unlist %>% matrix(nrow=2) %>% t days = days[,1] hours = as.POSIXlt(timestamps, origin = "1970-01-01") #%>% hours <- unclass(hours) reformat <- matrix(NA, nrow = length(hours[[1]]), ncol = length(hours)) for (i in 1:11) { reformat[,i] <- hours[[i]] } reformat <- apply(reformat, 2, as.numeric) #NA introduced by coercion reformat <- data.frame(reformat) #add variable names colnames(reformat) <- c("sec", "min", "hour","mday", "mon", "year","wday", "yday", "isdst", "zone", "gmtoff") hours <- reformat %>% dplyr::select(hour, min, sec) %>% apply(1, function(times) sum(times * c(1, 1/60, 1/3600) ) ) output = as_data_frame(cbind(hours=hours, days=days)) output["hours"] = lapply(output["hours"], as.numeric) return(output) }