数据帧和数据

发布于 2025-02-11 00:17:18 字数 286 浏览 2 评论 0原文

我有一个正在使用的数据集,我正在尝试更改时间列的格式。当前格式以这样的方式读取,例如:“ 2022-05-23 23:06:58”,我正在尝试更改此操作以显示我的时间并删除日期。

其他信息:我想在我的数据框架内进行此更改,而不仅仅是随机时间。我想更改超过100,000行,因此我需要一个功能或解决方案来这样做。整洁,橄榄酸,格式等。谢谢你们。

编辑:有一件事我可能没有充分阐明,我想保留确切的时间,什么也没有。因此,'23:48:07将是我寻找它的方式,而不仅仅是我们的方式。我需要它,以便最终可以减去两列之间的时间。你得到我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天荒地未老 2025-02-18 00:17:18

尝试一下
对于这里的第一个问题是要转换为一天小时的代码,

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


#which gives "23" hours of the day

因为您想在大型数据集上申请,我们在下面使用该代码

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

,其中大_df是您的大型数据集,价值超过100,000个记录
突变将为结果打开另一列,该列被命名为“小时”列
DateTime是您的大_DF数据集中的DateTime列

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

不即不离 2025-02-18 00:17:18

是字符串的时间吗?原因然后您可以使用substr像这样提取小时和分钟:

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使用100.000行时间列

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

柒七 2025-02-18 00:17:18
library(data.table)
hour("2022-05-23 23:06:58") # 23
library(data.table)
hour("2022-05-23 23:06:58") # 23
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文