带有前导 X 字符的迄今为止的字符串

发布于 2025-01-15 14:29:27 字数 596 浏览 1 评论 0原文

我正在尝试将 Date 列转换为日期格式,但我不断收到错误。我认为问题可能是日期是一个字符并且在年份之前有一个 X:

HMC.Close        Date
1     39.71 X2007.01.03
2     40.04 X2007.01.04
3     38.67 X2007.01.05
4     38.89 X2007.01.08
5     38.91 X2007.01.09
6     37.94 X2007.01.10

这是我一直在运行的代码:

stock_honda <- expand.grid("HMC" = HMC$HMC.Close) %>%
  "Date" = as.Date(row.names(as.data.frame(HMC))) %>% 
  subset(Date >"2021-02-28" & Date < "2022-03-11")

charToDate(x) 中的错误: 字符串不是标准的明确格式

I'm trying to convert the Date column to date format but I keep getting an error. I think the problem might be that the date is a character and has an X before the year:

HMC.Close        Date
1     39.71 X2007.01.03
2     40.04 X2007.01.04
3     38.67 X2007.01.05
4     38.89 X2007.01.08
5     38.91 X2007.01.09
6     37.94 X2007.01.10

This is the code I've been running:

stock_honda <- expand.grid("HMC" = HMC$HMC.Close) %>%
  "Date" = as.Date(row.names(as.data.frame(HMC))) %>% 
  subset(Date >"2021-02-28" & Date < "2022-03-11")

Error in charToDate(x) :
character string is not in a standard unambiguous format

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

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

发布评论

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

评论(2

別甾虛僞 2025-01-22 14:29:27

您可以使用 gsub 首先删除导致问题的“X”,然后使用 lubridate 包中的 ymd 将字符串转换为日期。此外,您可以使用 dplyr 包中的 mutate(across(...)) 进行转换,以 tidyverse 方式完成所有操作。

library(dplyr)
library(lubridate)

df |>
  # Mutate Date to remove X and convert it to Date
  mutate(across(Date, function(x){
    ymd(gsub("X","", x))
  }))

#  HMC.Close       Date
#1     39.71 2007-01-03
#2     40.04 2007-01-04
#3     38.67 2007-01-05
#4     38.89 2007-01-08
#5     38.91 2007-01-09
#6     37.94 2007-01-10

You can use gsub to first remove the "X" that is causing a problem and then use ymd from lubridate package to convert the strings into Dates. Additionally, you can make that conversion using mutate(across(...)) from the dplyr package to do everything in a tidyverse-way.

library(dplyr)
library(lubridate)

df |>
  # Mutate Date to remove X and convert it to Date
  mutate(across(Date, function(x){
    ymd(gsub("X","", x))
  }))

#  HMC.Close       Date
#1     39.71 2007-01-03
#2     40.04 2007-01-04
#3     38.67 2007-01-05
#4     38.89 2007-01-08
#5     38.91 2007-01-09
#6     37.94 2007-01-10
老娘不死你永远是小三 2025-01-22 14:29:27

这是一个首先避免在日期前添加“X”的管道:

library(quantmod)
getSymbols(c("FCAU.VI", "TYO", "VWAGY", "HMC"), na.rm = TRUE)

library(tidyverse)
stock_honda <- (HMC
  %>% as.data.frame()
  %>% rownames_to_column("Date")
  %>% select(Date, HMC.Close)
  %>% mutate(across(Date, lubridate::ymd))
  %>% filter(between(Date, as.Date("2021-02-28"), as.Date("2022-03-11")))
)

如果有一个 Between 版本可以避免显式转换为日期,那就太好了。 (filter("2021-02-28" < Date, Date < "2022-03-11") 也适用于最后一步。)

Here is a pipeline that avoids prepending "X" to the dates in the first place:

library(quantmod)
getSymbols(c("FCAU.VI", "TYO", "VWAGY", "HMC"), na.rm = TRUE)

library(tidyverse)
stock_honda <- (HMC
  %>% as.data.frame()
  %>% rownames_to_column("Date")
  %>% select(Date, HMC.Close)
  %>% mutate(across(Date, lubridate::ymd))
  %>% filter(between(Date, as.Date("2021-02-28"), as.Date("2022-03-11")))
)

It would be nice if there were a version of between that avoided the need to explicitly convert to dates. (filter("2021-02-28" < Date, Date < "2022-03-11") would also work for the last step.)

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