格式复杂字符串作为日期
我正在使用r
中的日期框架。我拥有的数据框架是下一步:
df
Date Q
1 Thu Nov 18 16:32:03 COT 2021 1
2 Tue Feb 01 23:28:14 COT 2022 1
3 Wed Feb 02 16:43:15 COT 2022 1
4 Sun Apr 03 22:17:28 COT 2022 1
5 Tue Apr 05 10:47:36 COT 2022 1
6 Thu Jul 01 16:52:52 COT 2021 1
使用dput()
:
#Dput
df <- structure(list(Date = c("Thu Nov 18 16:32:03 COT 2021", "Tue Feb 01 23:28:14 COT 2022",
"Wed Feb 02 16:43:15 COT 2022", "Sun Apr 03 22:17:28 COT 2022",
"Tue Apr 05 10:47:36 COT 2022", "Thu Jul 01 16:52:52 COT 2021"
), Q = c(1, 1, 1, 1, 1, 1)), row.names = c(NA, 6L), class = "data.frame")
主要问题是date
变量。如您所见,它具有非常长而复杂的格式。我尝试使用as.date()
甚至lubridate
,但我无法让代码工作以格式化日期。最后,我想拥有这样的东西:
Date Q NewDate
1 Thu Nov 18 16:32:03 COT 2021 1 2021-11-18
2 Tue Feb 01 23:28:14 COT 2022 1 2022-02-01
3 Wed Feb 02 16:43:15 COT 2022 1 2022-02-02
4 Sun Apr 03 22:17:28 COT 2022 1 2022-04-03
5 Tue Apr 05 10:47:36 COT 2022 1 2022-04-05
6 Thu Jul 01 16:52:52 COT 2021 1 2021-07-01
如果可能的话,我也想从变量date
中提取时间/小时。非常感谢。
I am working with a dataframe of dates in R
. The dataframe I have is next:
df
Date Q
1 Thu Nov 18 16:32:03 COT 2021 1
2 Tue Feb 01 23:28:14 COT 2022 1
3 Wed Feb 02 16:43:15 COT 2022 1
4 Sun Apr 03 22:17:28 COT 2022 1
5 Tue Apr 05 10:47:36 COT 2022 1
6 Thu Jul 01 16:52:52 COT 2021 1
With dput()
:
#Dput
df <- structure(list(Date = c("Thu Nov 18 16:32:03 COT 2021", "Tue Feb 01 23:28:14 COT 2022",
"Wed Feb 02 16:43:15 COT 2022", "Sun Apr 03 22:17:28 COT 2022",
"Tue Apr 05 10:47:36 COT 2022", "Thu Jul 01 16:52:52 COT 2021"
), Q = c(1, 1, 1, 1, 1, 1)), row.names = c(NA, 6L), class = "data.frame")
The main issue is about Date
variable. As you can see it has a very long and complex format. I have tried using as.Date()
or even lubridate
but I can not get the code working to format the dates. In the end I would like to have something like this:
Date Q NewDate
1 Thu Nov 18 16:32:03 COT 2021 1 2021-11-18
2 Tue Feb 01 23:28:14 COT 2022 1 2022-02-01
3 Wed Feb 02 16:43:15 COT 2022 1 2022-02-02
4 Sun Apr 03 22:17:28 COT 2022 1 2022-04-03
5 Tue Apr 05 10:47:36 COT 2022 1 2022-04-05
6 Thu Jul 01 16:52:52 COT 2021 1 2021-07-01
And if it is possible, I would also like to extract the time/hour from the variable Date
too. Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可能会使用
-output
如果
cot
零件不是恒定的,,请使用
sub
删除该子字符串,然后如果我们想要时间-Output
We may use
-output
If the
COT
part is not constant, remove that substring withsub
and then applyIf we want the time as well
-output