英文:
error while filtering the date from data frame
问题
我有一个包含多个日期列的大数据框,我试图在日期列中过滤出今天的日期,但无法过滤,因为我收到了下面的错误。
由as.POSIXlt.character()
中的错误引起:
!字符字符串不符合标准的明确格式
运行rlang::last_trace()
以查看错误发生的位置。
以下是日期格式,列的类别显示为字符
dd <- data.frame(c1 =c("1/16/2023 1:26", "1/16/2023 1:26", "1/16/2023 1:40", "1/16/2023 2:05",
"1/16/2023 2:05", "1/16/2023 2:10"))
dd %>% filter(as.date(c1) == lubridate::today())
dd %>% filter(as.date(as.character(c1) == lubridate::today()))
dd %>% filter(c1 == lubridate::today())
英文:
I have a big data frame with multiple date columns and i am trying filter date with todays date in date column but unable to filter as i was getting some error below.
Caused by error in as.POSIXlt.character()
:
! character string is not in a standard unambiguous format
Run rlang::last_trace()
to see where the error occurred.
below is the date format and class of the column is showing as character
dd <- data.frame(c1 =c("1/16/2023 1:26", "1/16/2023 1:26", "1/16/2023 1:40", "1/16/2023 2:05",
"1/16/2023 2:05", "1/16/2023 2:10"))
dd %>% filter(as.date(c1) == lubridate::today())
dd %>% filter(as.date(as.character(c1) == lubridate::today()))
dd %>% filter(c1 == lubridate::today())
答案1
得分: 2
以下是代码的翻译部分:
library(tidyverse)
dd <-
tibble(
c1 = c(
"1/16/2023 1:26",
"1/16/2023 1:26",
"1/16/2023 1:40",
"1/16/2023 2:05",
"1/16/2023 2:05",
"1/16/2023 2:10",
"5/25/2023 2:10"
)
)
dd %>%
mutate(across(c1, mdy_hm)) %>%
filter(date(c1) == today())
希望这有所帮助。
英文:
library(tidyverse)
dd <-
tibble(
c1 = c(
"1/16/2023 1:26",
"1/16/2023 1:26",
"1/16/2023 1:40",
"1/16/2023 2:05",
"1/16/2023 2:05",
"1/16/2023 2:10",
"5/25/2023 2:10"
)
)
dd %>%
mutate(across(c1, mdy_hm)) %>%
filter(date(c1) == today())
# A tibble: 1 x 1
c1
<dttm>
1 2023-05-25 02:10:00
答案2
得分: 2
你可以提供一个格式:
library(tidyverse)
library(lubridate)
dd <- data.frame(c1 =c("1/16/2023 1:26", "1/16/2023 1:26", "1/16/2023 1:40", "1/16/2023 2:05",
"1/16/2023 2:05", "1/16/2023 2:10"))
dd %>% filter(as.Date(c1, format = "%m/%d/%Y") == as.Date("2023-01-16"))
#> c1
#> 1 1/16/2023 1:26
#> 2 1/16/2023 1:26
#> 3 1/16/2023 1:40
#> 4 1/16/2023 2:05
#> 5 1/16/2023 2:05
#> 6 1/16/2023 2:10
dd %>% filter(as.Date(c1, format = "%m/%d/%y") == lubridate::today())
#> [1] c1
#> <0 rows> (or 0-length row.names)
创建于2023-05-25,使用 reprex v2.0.2
顺便提一下,as.date()
不存在,请尝试使您的可重现示例可重现。
英文:
You can provide a format:
library(tidyverse)
library(lubridate)
dd <- data.frame(c1 =c("1/16/2023 1:26", "1/16/2023 1:26", "1/16/2023 1:40", "1/16/2023 2:05",
"1/16/2023 2:05", "1/16/2023 2:10"))
dd %>% filter(as.Date(c1, format = "%m/%d/%Y") == as.Date("2023-01-16"))
#> c1
#> 1 1/16/2023 1:26
#> 2 1/16/2023 1:26
#> 3 1/16/2023 1:40
#> 4 1/16/2023 2:05
#> 5 1/16/2023 2:05
#> 6 1/16/2023 2:10
dd %>% filter(as.Date(c1, format = "%m/%d/%y") == lubridate::today())
#> [1] c1
#> <0 rows> (or 0-length row.names)
<sup>Created on 2023-05-25 with reprex v2.0.2</sup>
btw as.date()
doesn't exist, try to make your reproducible examples reproducible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论