【问题标题】:character string to date and time using R字符串到日期和时间使用R
【发布时间】:2015-11-24 01:25:37
【问题描述】:

我有大约 400,000 行的以下格式的日期和时间测验截止日期和提交时间。日期和时间是字符:

  Lesson<-data.table(Quiz = c(1:5), 
  Duedate=c("2015-07-29 08:00", "2015-08-05 08:00","2015-08-12 08:00", "2015-08-19 08:00", "2015-08-26 08:00"),
  Datecomp=c("2015-07-30 21:00", "2015-08-04 13:00","2015-08-12 07:59", "2015-08-19 10:00", "2015-08-16 23:00"))

我需要将其转换为日期和时间格式以使用

我尝试过使用

library(lubridate)
Lesson$Duedate<-as.Date(Lesson$Duedate,"%Y-%m-%d %H:%M")

但这会减少我需要的时间。

我也试过strptime如下:

Lesson$Datecomp<-strptime(Lesson$Datecomp, "%Y%m%d %H%M")

但我收到以下错误消息:

   Warning messages:
  1: In `[<-.data.table`(x, j = name, value = value) :
    Supplied 11 items to be assigned to 5 items of column 'Datecomp' (6 unused)
  2: In `[<-.data.table`(x, j = name, value = value) :
    Coerced 'list' RHS to 'character' to match the column's type. Either change the target column to 'list' first (by creating a new 'list' vector length 5 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.

任何有关如何格式化日期和时间以及指定澳大利亚/墨尔本时间的帮助将不胜感激。

【问题讨论】:

    标签: r strptime lubridate


    【解决方案1】:

    你想要:

    Lesson$Duedate <- as.POSIXct(Lesson$Duedate, tz = "Australia/Melbourne")
    Lesson$Datecomp <- as.POSIXct(Lesson$Datecomp, tz = "Australia/Melbourne")
    

    您可以在其中添加format = "%Y-%m-%d %H:%M",但您已经是默认格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多