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

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

error while filtering the date from data frame

问题

我有一个包含多个日期列的大数据框,我试图在日期列中过滤出今天的日期,但无法过滤,因为我收到了下面的错误。

as.POSIXlt.character()中的错误引起:
!字符字符串不符合标准的明确格式
运行rlang::last_trace()以查看错误发生的位置。

以下是日期格式,列的类别显示为字符

  1. 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",
  2. "1/16/2023 2:05", "1/16/2023 2:10"))
  3. dd %>% filter(as.date(c1) == lubridate::today())
  4. dd %>% filter(as.date(as.character(c1) == lubridate::today()))
  5. 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

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

  1. 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",
  2. "1/16/2023 2:05", "1/16/2023 2:10"))
  3. dd %>% filter(as.date(c1) == lubridate::today())
  4. dd %>% filter(as.date(as.character(c1) == lubridate::today()))
  5. dd %>% filter(c1 == lubridate::today())

答案1

得分: 2

以下是代码的翻译部分:

  1. library(tidyverse)
  2. dd <-
  3. tibble(
  4. c1 = c(
  5. "1/16/2023 1:26",
  6. "1/16/2023 1:26",
  7. "1/16/2023 1:40",
  8. "1/16/2023 2:05",
  9. "1/16/2023 2:05",
  10. "1/16/2023 2:10",
  11. "5/25/2023 2:10"
  12. )
  13. )
  14. dd %>%
  15. mutate(across(c1, mdy_hm)) %>%
  16. filter(date(c1) == today())

希望这有所帮助。

英文:
  1. library(tidyverse)
  2. dd &lt;-
  3. tibble(
  4. c1 = c(
  5. &quot;1/16/2023 1:26&quot;,
  6. &quot;1/16/2023 1:26&quot;,
  7. &quot;1/16/2023 1:40&quot;,
  8. &quot;1/16/2023 2:05&quot;,
  9. &quot;1/16/2023 2:05&quot;,
  10. &quot;1/16/2023 2:10&quot;,
  11. &quot;5/25/2023 2:10&quot;
  12. )
  13. )
  14. dd %&gt;%
  15. mutate(across(c1, mdy_hm)) %&gt;%
  16. filter(date(c1) == today())
  17. # A tibble: 1 x 1
  18. c1
  19. &lt;dttm&gt;
  20. 1 2023-05-25 02:10:00

答案2

得分: 2

你可以提供一个格式:

  1. library(tidyverse)
  2. library(lubridate)
  3. 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",
  4. "1/16/2023 2:05", "1/16/2023 2:10"))
  5. dd %>% filter(as.Date(c1, format = "%m/%d/%Y") == as.Date("2023-01-16"))
  6. #> c1
  7. #> 1 1/16/2023 1:26
  8. #> 2 1/16/2023 1:26
  9. #> 3 1/16/2023 1:40
  10. #> 4 1/16/2023 2:05
  11. #> 5 1/16/2023 2:05
  12. #> 6 1/16/2023 2:10
  13. dd %>% filter(as.Date(c1, format = "%m/%d/%y") == lubridate::today())
  14. #> [1] c1
  15. #> <0 rows> (or 0-length row.names)

创建于2023-05-25,使用 reprex v2.0.2

顺便提一下,as.date() 不存在,请尝试使您的可重现示例可重现。

英文:

You can provide a format:

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

确定