英文:
Re-format date and time in R
问题
我有两列数据(都是日期),我需要找出它们之间的天数(基本上是这两列之间的差异)。
其中一列的格式如下(从SPSS导入)- 我只包含了两行作为示例:
12-Mar-2020 15:51:32
06-Apr-2019 17:08:03
我需要它看起来像这样:
- 我需要一个新列,不包括时间,只包括日期(即去掉15:51:32和17:08:03)。
- 我需要将日期重新格式化为年-月-日的数字顺序,以便与我的另一列的格式匹配(即我需要将Mar变成03,将Apr变成04,还需要更改顺序)。
我尝试使用lubridate包,但由于这列的格式,它不成功(如果有帮助的话,这列的类别目前是“字符”)。
请注意:我的数据集相当大,所以我希望能一次在整个列上运行的解决方案。
我会感激你的帮助。
英文:
I have two columns of data (both dates), and I need to find the number of days in between (basically the difference between the columns).
One of the columns is formatted like this (imported from SPSS) - I only included two rows as example:
12-Mar-2020 15:51:32
06-Apr-2019 17:08:03
What I need this to look like:
- I need a new column that doesn't have the time, and it only includes the dates (i.e. get rid of 15:51:32 and 17:08:03).
- I need to reformat the dates as numbers in the order of year-month-date so that it matches the format of my other column (i.e. I need Mar to become 03 and Apr to 04, in addition to changing the order).
I tried using lubridate package, but it was not successful because of how this column is formatted (in case it's helpful, the class of this column is currently "character").
Please note: My data set is quite big, so I appreciate a solution that works on the entire column at once.
I would appreciate your help on this.
答案1
得分: 0
[1] "2020-03-12" "2019-04-06"
英文:
library(lubridate)
as_date(dmy_hms(c("12-Mar-2020 15:51:32", "06-Apr-2019 17:08:03")))
Result
[1] "2020-03-12" "2019-04-06"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论