How to convert time (mm:ss) to decimal form in R
Given that you start with a character vector, this is relatively easy :
minPerGame <- c("4:30","2:20","34:10")
sapply(strsplit(minPerGame,":"),
function(x) {
x <- as.numeric(x)
x[1]+x[2]/60
}
)
gives
[1] 4.500000 2.333333 34.166667
Make sure you checked that you used read.csv()
with the option as.is=TRUE
. Otherwise you'll have to convert using as.character()
.
How to convert time to decimal
There is probably a lubridate function for this, but I'd do it like this:
x <- "01:18:30"
y <- (as.numeric(as.POSIXct(paste("2014-01-01", x))) -
as.numeric(as.POSIXct("2014-01-01 0:0:0")))/60
#[1] 78.5
Ignoring the hours:
y%%60
#[1] 18.5
convert times to decimal hours or decimal minutes
You can do this with a bit of dplyr and tidyr code:
library(dplyr)
library(tidyr)
# I read your data to two different objects
df <- tibble(startTime, behavTime)
df_new <- df %>%
# your data is a factor (I assume from wrong options during reading it in)
mutate_if(is.factor, as.character) %>%
# We seperate the column into hours, minutes and seconds
separate(startTime, into = c("startTime_hh", "startTime_mm"), ":") %>%
separate(behavTime, into = c("behavTime_mm", "behavTime_ss"), ":") %>%
# We convert the time into fractions of an hour
mutate(startTime_hh = as.numeric(startTime_hh),
startTime_mm = as.numeric(startTime_mm) / 60,
behavTime_mm = as.numeric(behavTime_mm) / 60,
behavTime_ss = as.numeric(behavTime_ss) / 3600) %>%
# lastly we reassemble the times
mutate(startTime = startTime_hh + startTime_mm,
behavTime = behavTime_mm + behavTime_ss)
Is there an R function that can convert datetime field which is in decimal form (41183.50417) to dd/mm/yyyy hh:mm:ss
If you want to get rid of timezone display we can use format
format(as.POSIXct(as.Date(41183.50417, origin = '1900-01-01')), "%Y-%m-%d %T")
#[1] "2012-10-03 20:06:00"
Read ?strptime
for more format details.
How to convert decimal time to time format
Another option:
library(lubridate)
video$video_duration <- as.numeric(video_end - video_begin, units = "secs")
video$video_duration <- seconds_to_period(video$video_duration)
video_begin video_end video_duration
1 2017-09-12 00:08:14 2017-09-12 00:39:08 30M 54S
2 2017-09-12 00:04:47 2017-09-12 00:47:10 42M 23S
3 2017-09-12 00:08:27 2017-09-12 00:49:51 41M 24S
4 2017-09-12 00:04:59 2017-09-12 00:44:31 39M 32S
5 2017-09-12 00:04:57 2017-09-12 00:39:41 34M 44S
6 2017-09-12 00:07:51 2017-09-12 00:47:12 39M 21S
7 2017-09-12 00:06:11 2017-09-12 00:40:13 34M 2S
8 2017-09-12 00:05:30 2017-09-12 00:46:52 41M 22S
Convert column of time (as character decimals from Excel) to time in R
Found this answer here: How to express a decimal time in HH:MM
x <- as.numeric(env$Time) # Store time variable as numeric vector
env$Time2 <- sub(":00$", "", round(times(x), "min")) # Run this code to save as new column in dataframe (note, don't need to divide by 24 if decimal is fraction of a day like my data is
R - How to convert decimal to MM:SS.XXX
one option is the lubridate package - since you did not specify the output class I included a few possible outputs:
package(lubridate)
t <- 92.180
# your output string as character
lubridate::seconds(t) %>%
lubridate::as_datetime() %>%
format("%M:%OS3")
# output as period
lubridate::seconds(t) %>%
lubridate::as.period()
# output as duration
lubridate::seconds(t) %>%
lubridate::as.duration()
# output as time time
lubridate::seconds(t) %>%
lubridate::as.difftime()
Related Topics
Replace Empty Values with Value from Other Column in a Dataframe
Split the Title Onto Multiple Lines
Convert a Date Vector into Julian Day in R
Change Day of the Month in a Date to First Day (01)
Group by and Filter Data Management Using Dplyr
Typeof Returns Integer for Something That Is Clearly a Factor
Loop in R: How to Save the Outputs
Read and Rbind Multiple CSV Files
How to Plot Multiple Stacked Histograms Together in R
Divide Row Value by Aggregated Sum in R Data.Frame
Looping Through T.Tests for Data Frame Subsets in R
Rename Multiple Columns Given Character Vectors of Column Names and Replacement
R: What Do You Call the :: and ::: Operators and How Do They Differ
How to Add a Index by Set of Data When Using Rbindlist
How to Define More Line Types for Graphs in R (Custom Linetype)