【问题标题】:mutate_impl error in dplyr/lubridate add date timedplyr/lubridate 中的 mutate_impl 错误添加日期时间
【发布时间】:2017-10-05 10:24:49
【问题描述】:

使用 lubridate 包,我想在 tibble 中的 "POSIXct", "POSIXt" 字段中添加秒数(出于示例的目的)。

b <- structure(list(`"a"` = c("a", "a", "a", "a", "a"), Date_time = structure(c(1506694322, 
1506694270, 1506693970, 1506693897, 1506693849), class = c("POSIXct", 
"POSIXt"), tzone = "")), .Names = c("\"a\"", "Date_time"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

b %>%
  mutate(tol_lower = Date_time - second(2),
         tol_lower = Date_time + second(30))

我得到错误:

Error in mutate_impl(.data, dots) : 'origin' must be supplied

这是为什么?我很感激我可以计算小时数,但我想知道我做错了什么。

补充点:

-我试过 as.Date,还是一样的错误。

-我可以直接添加秒数而不会出现问题:tol_lower = Date_time - 2

【问题讨论】:

    标签: r datetime dplyr lubridate tidyverse


    【解决方案1】:

    为什么不用这个?

    b %>% mutate(tol_lower = Date_time - 2,
                 tol_upper = Date_time + 30)
    

    如果您想将小时数添加到给定日期,则只需使用 Date_time + 2*60*60(即,将 2 小时添加到 Date_time)。


    另外?second 明确表示second(x) 中的x 是一个“日期时间对象”,但在您的情况下,您正在尝试传递一个整数。

    希望对你有帮助!

    【讨论】:

    • 这很好,但可能你应该详细说明一下.. :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多