英文:
unexpected error - format datetime into mutate
问题
这个解决方案导致错误
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 defaultformat=, which is to try some unambiguous formats embedded within the function. Notably missing from the list oftryFormats=is anything including"%d.%m.%Y ...".- Convert back to a string with
format(y, format="%d.%m.%Y %H:%M:%S"). This assumes thatyhere already inherits fromPOSIXt, so that this makes sense. This expression should result in strings (characterclass), not number-like timestamps (POSIXtclass).
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 thatxhere is not in an unambiguous format, so you are tellingas.POSIXcthow to find specific components of the timestamp. This should result in a number-likePOSIXt-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论