-1

I have a dataset that I’m working with and I’m trying to change the format of my time column. The current format reads like this, example: “2022-05-23 23:06:58”, I’m trying to change this to only show me the hour times and erase the dates.

Other info: I want to make this change within my data frame, not just random times. I want to change over 100,000 rows so I need a function or solution that will do so. Tidyverse, Lubridate, Format, etc. Thank you guys.

Edit: There was one thing I may not have articulated fully, I wanted to keep the exact time and nothing else. so ‘23:48:07 would’ be how I’m looking for it not just the our. I need it so I can eventually subtract the time passed between two columns. You get me?

3 Answers3

2

Try this for the first question here is the code to convert to the hour of the day

your_time<-format(as.POSIXct(your_time), format = "%H:%M:%S")


#which gives "23" hours of the day

Since you want to apply on a large dataset we use this below

large_df%>%
  mutate(Hour = format(as.POSIXct(Datetime), format ="%H:%M:%S"))

where the large_df is your large dataset worth over 100,000 records The mutate will open another column for the result which is named the Hour column and the Datetime is the DateTime column in your large_df dataset

Yomi.blaze93
  • 401
  • 3
  • 10
  • Thank you so much. There was one thing I may not have articulated fully, I wanted to keep the exact time and nothing else. so ‘23:48:07 would’ be how I’m looking for it not just the our. I need it so I can eventually subtract the time passed between two columns. You get me? – BlockBilz11 Jun 28 '22 at 13:40
  • Ok If that's what you want, you need to change the format from "%H" to this format "%H:%M:%S" see the script edited – Yomi.blaze93 Jun 28 '22 at 13:47
  • You are amazing. Thank you. I’m gonna follow you cause I’m just getting started with R and you seem like a good person to ask. – BlockBilz11 Jun 28 '22 at 15:29
  • You're always welcome. don't forget to upvote my answer as it has helped you solve your problem. – Yomi.blaze93 Jun 28 '22 at 20:11
  • It says I need a reputation of at least 15, this was my first interaction so I can’t yet but I won’t forget about you! I have one more question how ever. I have column in my dataset: “Rider_type” and the answer for each rider is either member or non member. How do create two separate columns so the members and non members are in separate columns? – BlockBilz11 Jun 28 '22 at 21:04
  • could you share a sample of your question .....its always good to share a sample and the end result for guidance – Yomi.blaze93 Jun 28 '22 at 22:24
  • Code B A…. Say there’s a pattern of this. Each row for this column would either be A or B. I want to create to separate columns so my B results are in one column & my A results are in another. I’m typing on a phone so it may not have formatted how a want. Say row 1A is the column name: “Code”. Row 2A is: “A”, Row 3A is “B”. And the subsequent rows have similar answers. I want to separate them. I Hope that makes sense. – BlockBilz11 Jun 29 '22 at 03:35
  • what exactly do you want to separate – Yomi.blaze93 Jun 29 '22 at 03:48
  • I want it some the users or rows whose answers are “A” to be in one column. & the users or rows with answers “B” to be in a separate column. They’re in one column now, I want them in two separate columns. – BlockBilz11 Jun 29 '22 at 05:32
  • Try and ask the question in another thread, not this thread since the initial question has been answered. you can tag me with the new question. – Yomi.blaze93 Jun 29 '22 at 08:06
  • I think I tagged you, if not, here’s the link to the question https://stackoverflow.com/q/72803017/19303355 – BlockBilz11 Jun 29 '22 at 14:22
0

Is the time as a string ok? Cause then you can use substr to extract the hour and minutes like so:

time <- c("2022-05-23 23:02:58", "2022-05-23 13:52:58", "2022-05-23 03:31:58", "2022-05-23 09:09:58")
n <- nchar(time)
hour <- substr(time, n - 7, n - 3)

Just time with your 100.000 row time column

brendbech
  • 399
  • 1
  • 7
  • Hi, I can’t use it as a string because I’m converted two different columns in to the hour “23:04:51” format in order to subtract them to find time passed, so I can’t use strings. – BlockBilz11 Jun 28 '22 at 14:31
0
library(data.table)
hour("2022-05-23 23:06:58") # 23
s_baldur
  • 29,441
  • 4
  • 36
  • 69