意外错误 – 将日期时间格式化为变异

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

unexpected error - format datetime into mutate

问题

我正在跟踪线索:
https://stackoverflow.com/questions/63581912/mutate-and-format-multiple-date-columns/63582008#63582008

这个解决方案导致错误

M601_M_multi.2 <- M601_M_multi %>% 
  mutate (
    across(ends_with(' Time'), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S")),
                 .names = "{.col}.used")   
  )
Error in `mutate()`:
ℹ In argument: `across(contains(" Time"), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S"))`.
Caused by error in `across()`:
! Can't compute column `C3_S1_O2_PV Time`.
Caused by error in `as.POSIXlt.character()`

在数据框中,我有许多以" Time"结尾的列。我想将它们全部转换为日期和时间(as.POSIXct)

单列解决方案有效

M601_M_multi.2 <- M601_M_multi %>% 
  mutate (
    test = as.POSIXct(`C3_S1_O2_PV Time`, format ="%d.%m.%Y %H:%M:%S")    
  )

任何建议和答案都将不胜感激。

英文:

I was following the clue:
https://stackoverflow.com/questions/63581912/mutate-and-format-multiple-date-columns/63582008#63582008

The solution results in an error

M601_M_multi.2 <- M601_M_multi %>% 
  mutate (
    across(ends_with(' Time'), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S")),
                 .names = "{.col}.used")   
  )
Error in `mutate()`:
ℹ In argument: `across(contains(" Time"), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S"))`.
Caused by error in `across()`:
! Can't compute column `C3_S1_O2_PV Time`.
Caused by error in `as.POSIXlt.character()`

In df I have numerous columns where the name ends with " Time". I want to convert all to date and time (as.POSIXct)

The single column solution works though

M601_M_multi.2 <- M601_M_multi %>% 
  mutate (
    test = as.POSIXct(`C3_S1_O2_PV Time`, format ="%d.%m.%Y %H:%M:%S")    
  )

Any suggestion and answer will be appreciated

答案1

得分: 2

您的代码块不等效。

  mutate (
    across(ends_with(' Time'), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S")),
                 .names = "{.col}.used")   
  )

代码序列如下:

  • as.POSIXct(x),使用默认的 format=,尝试在函数内部嵌入一些明确的格式。在 tryFormats= 的格式列表中明显缺失的是包含 "%d.%m.%Y ..." 的任何内容。
  • 使用 format(y, format="%d.%m.%Y %H:%M:%S") 将其转换回字符串。这假定这里的 y 已经继承自 POSIXt,以便这样做有意义。这个表达式应该产生字符串(character 类型),而不是类似于时间戳的数字(POSIXt 类型)。

在您的第二个代码块中:

  mutate (
    test = as.POSIXct(`C3_S1_O2_PV Time`, format ="%d.%m.%Y %H:%M:%S")    
  )

代码序列如下:

  • as.POSIXct(x, format="%d.%m.%Y %H:%M:%S") 在这里是正确的(我认为),因为您知道这里的 x 不是一个明确的格式,所以您告诉 as.POSIXct 如何查找时间戳的特定组件。这应该产生类似于数字的 POSIXt 类对象。

我认为您的代码应该是:

  mutate(
    across(ends_with(' Time'), ~ as.POSIXct(., format="%d.%m.%Y %H:%M:%S"),
           .names = "{.col}.used")   
  )

这将生成新的列(在列名后附加了 ".used"),其值为 POSIXt 类型。

英文:

Your code blocks are not equivalent.

  mutate (
    across(ends_with(' Time'), ~format(as.POSIXct(.), "%d.%m.%Y %H:%M:%S")),
                 .names = "{.col}.used")   
  )

The code sequence:

  • as.POSIXct(x), using the default format=, which is to try some unambiguous formats embedded within the function. Notably missing from the list of tryFormats= is anything including "%d.%m.%Y ...".
  • Convert back to a string with format(y, format="%d.%m.%Y %H:%M:%S"). This assumes that y here already inherits from POSIXt, so that this makes sense. This expression should result in strings (character class), not number-like timestamps (POSIXt class).

In your second block:

  mutate (
    test = as.POSIXct(`C3_S1_O2_PV Time`, format ="%d.%m.%Y %H:%M:%S")    
  )

The code sequence:

  • as.POSIXct(x, format="%d.%m.%Y %H:%M:%S") is correct (I think) in that you know that x here is not in an unambiguous format, so you are telling as.POSIXct how to find specific components of the timestamp. This should result in a number-like POSIXt-class object.

I think you need your code to be:

  mutate(
    across(ends_with(' Time'), ~ as.POSIXct(., format="%d.%m.%Y %H:%M:%S"),
           .names = "{.col}.used")   
  )

which results in new columns (with .used appended to the names) filled with POSIXt-class values.

huangapple
  • 本文由 发表于 2023年8月4日 03:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831079.html
匿名

发表评论

匿名网友

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

确定