重新格式化日期和时间在R中

huangapple go评论64阅读模式
英文:

Re-format date and time in R

问题

我有两列数据(都是日期),我需要找出它们之间的天数(基本上是这两列之间的差异)。

其中一列的格式如下(从SPSS导入)- 我只包含了两行作为示例:
12-Mar-2020 15:51:32
06-Apr-2019 17:08:03

我需要它看起来像这样:

  1. 我需要一个新列,不包括时间,只包括日期(即去掉15:51:32和17:08:03)。
  2. 我需要将日期重新格式化为年-月-日的数字顺序,以便与我的另一列的格式匹配(即我需要将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:

  1. 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).
  2. 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"

huangapple
  • 本文由 发表于 2023年3月8日 14:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75669923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定