Converting char to time (duration(hms))

I'm attempting to change the column called Duration from char to time:

structure(list(RouteID = c(12817402, 12817404, 12817406, 12817425,
12817426, 12817446, 12817447, 12817455, 12817481, 12817499, 12817599,
12817603, 12817631, 12817636, 12817655), PaymentPlan = c("Subscriber",
"Casual", "Subscriber", "Casual", "Casual", "Casual", "Subscriber",
"Subscriber", "Casual", "Casual", "Subscriber", "Casual", "Casual",
"Casual", "Casual"), StartHub = c("NW Johnson at Jamison Square",
"SE Ladd at Hawthorne", NA, "SE 50th at Clinton", NA, "SE 30th at Division",
"SW Morrison at 18th", "NE 42nd at Hancock", NA, NA, "SW River at Montgomery",
NA, "NW Flanders at 14th", NA, NA), StartLatitude = c(45.5286366,
45.5120818, 45.522783, 45.503506, 45.5086555, 45.50468892, 45.52196048,
45.536898, 45.5041753, 45.5014807, 45.50910258, 45.5233209, 45.52579919,
45.5315017, 45.5218677), StartLongitude = c(-122.6820195, -122.6533493,
-122.6811195, -122.611066, -122.6547299, -122.6345551, -122.6896772,
-122.619969, -122.6612413, -122.6557978, -122.6735169, -122.6963404,
-122.6855063, -122.6834541, -122.6747676), StartDate = c("12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019"), StartTime = structure(c(1020,
1140, 1200, 2700, 2880, 6660, 6960, 8280, 13020, 15900, 23040,
23340, 24780, 24840, 25500), class = c("hms", "difftime"), units = "secs"),
EndHub = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "NW Couch at 11th",
"SE 2nd Pl at Tilikum Way", "NW Raleigh at 21st", NA, "SW 5th at Morrison"
), EndLatitude = c(45.524531, 45.5086555, 45.5090834, 45.5422432,
45.5041753, 45.5034396, 45.5312952, 45.5317187, 45.5014807,
45.5041753, 45.52374151, 45.50624163, 45.53409115, 45.5144089,
45.51889487), EndLongitude = c(-122.6744613, -122.6547299,
-122.6840225, -122.604573, -122.6612413, -122.639666, -122.6946193,
-122.6306539, -122.6557978, -122.6612413, -122.6818129, -122.6633379,
-122.6949424, -122.6840143, -122.6774061), EndDate = c("12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019",
"12/1/2019", "12/1/2019", "12/1/2019", "12/1/2019"), EndTime = structure(c(1740,
1440, 1860, 4200, 3780, 6840, 8220, 10560, 13380, 16500,
23880, 24420, 25140, 26100, 25800), class = c("hms", "difftime"
), units = "secs"), TripType = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), BikeID = c(6516, 24819, 7342, 6636, 24819,
7109, 6396, 6082, 24819, 24819, 6464, 7252, 6310, 6040, 6249
), BikeName = c("0060 BIKETOWN", "0199 BIKETOWN", "1016 LATINX HERITAGE MONTH",
"0825 BIKETOWN", "0199 BIKETOWN", "0749 BIKETOWN", "0707 BIKETOWN",
"0084 BIKETOWN", "0199 BIKETOWN", "0199 BIKETOWN", "0559 BIKETOWN",
"0845 BIKETOWN", "0868 BIKETOWN", "0901 BIKETOWN", "0300 BIKETOWN"
), Distance_Miles = c(0.72, 0.44, 1.18, 2.99, 1.36, 0.31,
0.89, 0.71, 0.58, 0.83, 1.5, 2.46, 1.02, 1.66, 0.34), Duration = c("0:11:10",
"0:05:18", "0:11:31", "0:24:28", "0:15:11", "0:03:11", "0:21:02",
"0:37:45", "0:05:38", "0:10:26", "0:13:33", "0:17:27", "0:06:42",
"0:20:58", "0:04:51"), RentalAccessPath = c("keypad", "mobile",
"mobile", "keypad", "mobile", "mobile", "keypad", "mobile",
"mobile", "mobile", "keypad", "keypad", "keypad", "keypad",
"keypad_rfid_card"), MultipleRental = c(FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE)), row.names = c(NA, -15L), class = c("tbl_df",
"tbl", "data.frame"))

I ran this:

library(lubridate)
X2020_07 <- as.duration(hms(X2020_07$Duration))

where X df is structured similar to the dput df above.
The type was changed from char to time but there is only 1 column!:

new("Duration", .Data = c(1687, 73, 1499, 691, 475, 350, 538,
3018, 1594, 2447, 1160, 1185, 469, 1090, 424))

If the previous description is inadequate, the goal is to preserve the 19 column df when it's piped to another df of the same name , i.e. X2020_07 <- X2020 while changing the column type to time.

Given my limited knowledge of R (I am enjoying learning) unsure what else I should be looking into. Appreciate guidance and solutions!

Hello, you can mutate additional columns to an existing dataset fairly easily using a | > or %>% after data processing .

Naming the mutated variable the same as the old one will over-write the original, but this is usually not recommended. There are cheat sheets on Rstudio you could find and here is a basic pipe tutorial: Simplify Your Code with %>% ยท UC Business Analytics R Programming Guide

library(dplyr)
library(lubridate)

create_df <-
time_df %>% #name of dataframe 
  mutate(
    duration = as.duration(hms(Duration)) #creates new column
    #Duration = as.duration(hms(Duration)) , if you want to over-write old column
  )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.