如何在 R 编程中将日期时间更改为新列中的日期?
我对 R 很陌生,但正在尽我最大的努力学习!需要帮助添加仅用于从日期时间数据解析的日期的列。
我的数据如下所示:
city | state | datetime
Sand | CA. | 2017-12-22 14:07:00
我想添加第四列,其中仅包含日期。我以为我可以使用 Lubridate,但函数 ymd_hms("datetime")
不会解析数据。
感谢您的帮助
I am very new to R but trying my best to learn! Need help adding a column for just the date, parsed from datetime data.
My data looks like this:
city | state | datetime
Sand | CA. | 2017-12-22 14:07:00
I would like to add a fourth column with just the date. I thought I could use Lubridate but the function ymd_hms("datetime")
will not parse the data.
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
as.Date
从日期和时间中提取日期。或者,如果您的日期时间列只是一个字符,那么您需要先转换为 POSIXct。
或者以 R 为基数:
输出
数据
You can just use
as.Date
to pull out the date from the date and time.Or if your date time column is just a character, then you need to convert to POSIXct first.
Or in base R:
Output
Data
如果您已将数据存储在名为 data 的数据框中,那么下面的代码用于在数据框中添加新的日期列。
if you have store the data in dataframe named data then below is code to add a new column of date in your dataframe.