I have the below times in a column and would like to change the standalone mm:ss values to 00:mm:ss. The final output must be numeric.
I am not sure where to start here. I think that gsub() may be an appropriate solution, but I am not sure of the syntax to only add to the standalone mm:ss values.
This is what I see in excel
Name Section Role Last Activity Total Activity
A biology Student Feb 8 at 1:08pm 18:16
B biology Student Feb 8 at 1:37pm 3:22:10
C biology Student Feb 8 at 10:37pm 9:51
D biology Student Feb 8 at 11:50am 5:32:31
E biology Student Feb 9 at 12:08pm 7:17:49
F biology Student Feb 10 at 1:33am 12:25:41
This is what I see when I import this into R
structure(list(Name = c("A", "B", "C", "D", "E", "F"), Section = c("biology",
"biology", "biology", "biology", "biology", "biology"), Role = c("Student",
"Student", "Student", "Student", "Student", "Student"), `Last Activity` = c("Feb 8 at 1:08pm",
"Feb 8 at 1:37pm", "Feb 8 at 10:37pm", "Feb 8 at 11:50am", "Feb 9 at 12:08pm",
"Feb 10 at 1:33am"), `Total Activity` = structure(c(-2209009440,
-2209063070, -2209039740, -2209055249, -2209048931, -2209030459
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
The following is a preliminary solution to your problem. Note that these values are not numeric since there is no number 25:12
etc. If you want to calculate on them, you may transform them do dates, eg POSIX-classes.
Data
ex <- c("20:28",
"18:53",
"25:01:00",
"17:55",
"27:04:00",
"24:43:00")
Code
ex[stringr::str_count(ex, ":") == 1] <- gsub("^",
"00:",
ex[stringr::str_count(ex, ":") == 1])
Output
> ex
[1] "00:20:28" "00:18:53" "25:01:00"
[4] "00:17:55" "27:04:00" "24:43:00"
You can count number of character in the data and prepend '00:'
if it is less than 8.
df <- data.frame(time = c("20:28", "18:53", "25:01:00", "17:55", "27:04:00", "24:43:00"))
df <- transform(df, time = ifelse(nchar(time) < 8, paste0('00:', time), time))
df
# time
#1 00:20:28
#2 00:18:53
#3 25:01:00
#4 00:17:55
#5 27:04:00
#6 24:43:00
For the updated dataset we can turn the column Last Activity
into date-time and use format
.
library(lubridate)
library(dplyr)
df <- df %>%
mutate(`Last Activity` = format(ymd_hm(paste('2020', `Last Activity`)), '%T'),
`Total Activity` = format(`Total Activity`, '%T'))
df
# Name Section Role `Last Activity` `Total Activity`
# <chr> <chr> <chr> <chr> <chr>
#1 A biology Student Feb 8 at 1:08pm 18:16:00
#2 B biology Student Feb 8 at 1:37pm 03:22:10
#3 C biology Student Feb 8 at 10:37pm 09:51:00
#4 D biology Student Feb 8 at 11:50am 05:32:31
#5 E biology Student Feb 9 at 12:08pm 07:17:49
#6 F biology Student Feb 10 at 1:33am 12:25:41
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.