从数据框中筛选日期时出现错误。

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

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 &lt;-
  tibble(
    c1 = c(
      &quot;1/16/2023 1:26&quot;,
      &quot;1/16/2023 1:26&quot;,
      &quot;1/16/2023 1:40&quot;,
      &quot;1/16/2023 2:05&quot;,
      &quot;1/16/2023 2:05&quot;,
      &quot;1/16/2023 2:10&quot;, 
      &quot;5/25/2023 2:10&quot;
    )
  ) 

dd %&gt;%  
  mutate(across(c1, mdy_hm)) %&gt;% 
  filter(date(c1) == today())

# A tibble: 1 x 1
  c1                 
  &lt;dttm&gt;             
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 &lt;- data.frame(c1 =c(&quot;1/16/2023 1:26&quot;, &quot;1/16/2023 1:26&quot;, &quot;1/16/2023 1:40&quot;, &quot;1/16/2023 2:05&quot;, 
                       &quot;1/16/2023 2:05&quot;, &quot;1/16/2023 2:10&quot;))

dd %&gt;% filter(as.Date(c1, format = &quot;%m/%d/%Y&quot;) == as.Date(&quot;2023-01-16&quot;))
#&gt;               c1
#&gt; 1 1/16/2023 1:26
#&gt; 2 1/16/2023 1:26
#&gt; 3 1/16/2023 1:40
#&gt; 4 1/16/2023 2:05
#&gt; 5 1/16/2023 2:05
#&gt; 6 1/16/2023 2:10
dd %&gt;% filter(as.Date(c1, format = &quot;%m/%d/%y&quot;) == lubridate::today())
#&gt; [1] c1
#&gt; &lt;0 rows&gt; (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.

huangapple
  • 本文由 发表于 2023年5月25日 16:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330224.html
匿名

发表评论

匿名网友

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

确定