使用strptime后,使用Posixct从CHR到DateTime更改

发布于 2025-01-31 20:49:43 字数 823 浏览 2 评论 0原文

我正在尝试更改一些具有当前具有字符格式的日期时间信息的列的数据帧。例如,

$ Date <chr> "5/2/2016 23:59", "5/3/2016 23:59", "4/13/2016 1:08", "4/21/2016 23:59"

这就是我所做的。

weightLog_data[['Date']] <- format(strptime(weightLog_data[['Date']], "%m/%d/%Y %H:%M"), "%Y/%m/%d %I:%M %p")

这成功将AM/PM的常规时间转换为y/m/d,但仍处于字符格式。 $日期“ 2016/05/02 11:59 PM”,“ 2016/05/03 11:59 PM”,“ 2016/04/13 01:08 AM” 然后,我尝试使用此功能。

weightLog_data[['Date']] <- as.POSIXct(weightLog_data[['Date']], format="%Y/%m/%d %I:%M %p")

但是,我

$ Date <dttm> 2016-05-02 23:59:00, 2016-05-03 23:59:00, 2016-04-13 01:08:00, 2016-04-21 23:59:00

现在收到了现在/下午的失踪,它又恢复了军事。我该如何解决并不以军事形式并包括AM/PM?

I am attempting to change a few data frames that have columns with date time information that are currently in character format. For instance

$ Date <chr> "5/2/2016 23:59", "5/3/2016 23:59", "4/13/2016 1:08", "4/21/2016 23:59"

This is what I have done.

weightLog_data[['Date']] <- format(strptime(weightLog_data[['Date']], "%m/%d/%Y %H:%M"), "%Y/%m/%d %I:%M %p")

This successfully converts to Y/m/d regular time with am/pm HOWEVER it is still in character format.
$ Date "2016/05/02 11:59 PM", "2016/05/03 11:59 PM", "2016/04/13 01:08 AM"
I then try to use this function.

weightLog_data[['Date']] <- as.POSIXct(weightLog_data[['Date']], format="%Y/%m/%d %I:%M %p")

HOWEVER, I receive

$ Date <dttm> 2016-05-02 23:59:00, 2016-05-03 23:59:00, 2016-04-13 01:08:00, 2016-04-21 23:59:00

so now am/pm is missing and it is back to military. How do I fix to be and not be in military format and include am/pm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2025-02-07 20:49:43

Posixct只是以这种方式显示。如果您更改格式以使其以某种方式打印,它将不再是Posixct。如果您想在R中的日期时间工作,则可以与Posixct合作。当需要打印,显示或绘制数据时,您可以将其转换回字符。正如他们所说,你不能吃蛋糕吃它。

或者至少,并非一点点努力。可以创建自己的S3类,该类从Posixct继承,因此您仍然可以像Posixct对象一样操纵它。这样的班级的草稿可能是这样的:

as_datetime <- function(x) {
  x <- as.POSIXct(strptime(x, format = '%m/%d/%Y %H:%M'))
  structure(as.numeric(x), class = c("datetime", "POSIXct", "POSIXt"),
            tz = attr(x, 'tz'))
}

as.POSIXct.datetime <- function(x, ...) {
  class(x) <-  c("POSIXct", "POSIXt")
  x
}

format.datetime <- function(x, ...) format(as.POSIXct(x), "%Y/%m/%d %I:%M %p")

现在您可以以角色格式喂食数据:

d <- c("5/2/2016 23:59", "5/3/2016 23:59", "4/13/2016 1:08", "4/21/2016 23:59")

as_datetime(d)
#> [1] "2016/05/02 11:59 PM" "2016/05/03 11:59 PM" "2016/04/13 01:08 AM"
#> [4] "2016/04/21 11:59 PM"

data.frame(date = as_datetime(d))
#>                  date
#> 1 2016/05/02 11:59 PM
#> 2 2016/05/03 11:59 PM
#> 3 2016/04/13 01:08 AM
#> 4 2016/04/21 11:59 PM

但是,尝试使用此类会有很多障碍和陷阱。您需要包括一个ops方法,以防止在操纵它时恢复Posixct,并且某些功能可能会检查类属性并拒绝使用新类运行。同样,所有这些都可以解决所有问题,但是恕我直言,您应该习惯于使用Posixct,然后当您希望将数据显示给其他人时,将其格式化,但是您请作为最后一步

POSIXct just displays that way. If you change the format to get it to print in a certain way, it won't be POSIXct any more. If you want to work in date times in R, you work with POSIXct. When it comes time to print, display, or plot your data, you can convert it back to character. You can't have your cake and eat it, as they say.

Or at least, not without a little effort. It is possible to create your own S3 class that inherits from POSIXct so you can still manipulate it like a POSIXct object. A very rough draft of such a class might be something like this:

as_datetime <- function(x) {
  x <- as.POSIXct(strptime(x, format = '%m/%d/%Y %H:%M'))
  structure(as.numeric(x), class = c("datetime", "POSIXct", "POSIXt"),
            tz = attr(x, 'tz'))
}

as.POSIXct.datetime <- function(x, ...) {
  class(x) <-  c("POSIXct", "POSIXt")
  x
}

format.datetime <- function(x, ...) format(as.POSIXct(x), "%Y/%m/%d %I:%M %p")

Now you can feed your data in as character format:

d <- c("5/2/2016 23:59", "5/3/2016 23:59", "4/13/2016 1:08", "4/21/2016 23:59")

as_datetime(d)
#> [1] "2016/05/02 11:59 PM" "2016/05/03 11:59 PM" "2016/04/13 01:08 AM"
#> [4] "2016/04/21 11:59 PM"

data.frame(date = as_datetime(d))
#>                  date
#> 1 2016/05/02 11:59 PM
#> 2 2016/05/03 11:59 PM
#> 3 2016/04/13 01:08 AM
#> 4 2016/04/21 11:59 PM

However, there will be a lot of snags and gotchas with trying to use this class. You would need to include an Ops method to prevent reversion to POSIXct when manipulating it, and some functions might check class attributes and refuse to run with your new class. Again, there are work arounds for all of this, but IMHO you should just get used to using POSIXct, then when you wish to display the data to someone else, format it however you please as the final step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文