【问题标题】:trying to extract date from timestamp R试图从时间戳 R 中提取日期
【发布时间】:2021-12-30 06:03:39
【问题描述】:

我有一个带列的 df

id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
time = c("2020-12-31 16:00:00 PST", "2020-12-31 16:00:00 PST", "2020-12-31 16:00:00 PST", "2020-12-31 16:00:01 PST", "2020-12-31 16:00:01 PST", "2020-12-31 16:00:02 PST", "2020-12-31 16:00:03 PST", "2020-12-31 16:00:03 PST", "2020-12-31 16:00:03 PST", "2020-12-31 16:00:03 PST")

df = data.frame(id, time)

我在原始时间戳列上使用了anytime() 函数来提取时间,因此这些值是双倍的。我想只保留列中的日期并删除时间和时区。例如,时间列中的第一个值将是“2020-12-31”。有没有快速的方法来做到这一点?

原来我在做:

df$'date' = as.character(anytime(df$time)) 

df = df %>% 
  mutate(date = str_split_fixed(df$date, " ", 2)) %>%
  mutate(date = as.Date(date))

但是,将每个值转换为字符串,然后根据空格进行拆分,然后将日期转换回日期格式需要一段时间(这也会导致维度出现问题,因为数据列仅显示日期,但将该列转换为 as.Date() 显示有问题。有更快的方法吗?谢谢!

【问题讨论】:

  • 您发现anytime 可能只是切换到anydate?同样的包装,同样的味道:)

标签: r date time extract


【解决方案1】:

as.Date 将采用一个字符串,即使它包含时间。无需在确定日期之前将其删除。

as.Date("2020-12-31 16:00:00 PST")
# [1] "2020-12-31"

使用您的代码和数据

df = df %>% 
  mutate(date = as.Date(time))

#    id                    time       date
# 1   1 2020-12-31 16:00:00 PST 2020-12-31
# 2   2 2020-12-31 16:00:00 PST 2020-12-31
# 3   3 2020-12-31 16:00:00 PST 2020-12-31
# 4   4 2020-12-31 16:00:01 PST 2020-12-31
# 5   5 2020-12-31 16:00:01 PST 2020-12-31
# 6   6 2020-12-31 16:00:02 PST 2020-12-31
# 7   7 2020-12-31 16:00:03 PST 2020-12-31
# 8   8 2020-12-31 16:00:03 PST 2020-12-31
# 9   9 2020-12-31 16:00:03 PST 2020-12-31
# 10 10 2020-12-31 16:00:03 PST 2020-12-31

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2014-11-20
    • 2017-02-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多