【问题标题】:r : how to make tribble with IDater : 如何用 IDate 制作 tribble
【发布时间】:2023-01-24 19:35:06
【问题描述】:

你愿意告诉我如何修复我的代码吗? 我想将 D.LasrPV 和 D.LastCEMI 列作为 IDate,但下面的代码给我错误。

tribble(
    ~SUBJID, ~D.LastPV,  ~D.LastCEMI,
    2610041, as.IDate('2022/05/23'), as.IDate('09/05/2022'),
    2618012, as.IDate('2022/09/02'), as.IDate('09/02/2022'),
    2641012, as.IDate('2022/07/25'), as.IDate('08/29/2022'))

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

【问题讨论】:

  • 至少告诉我们错误。
  • 对不起@paul我编辑了我的问题

标签: r


【解决方案1】:

逐行测试你的 reprex:

> as.IDate('2022/05/23')
[1] "2022-05-23"
>   as.IDate('09/05/2022')
[1] "0009-05-20"
> as.IDate('2022/09/02')
[1] "2022-09-02"
>   as.IDate('09/02/2022')
[1] "0009-02-20"
> as.IDate('2022/07/25')
[1] "2022-07-25"
>   as.IDate('08/29/2022')
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

as.IDate() 似乎没有按照您的意愿行事。

通过指定日期格式修复:

testframe <-  tribble(
    ~SUBJID, ~D.LastPV,  ~D.LastCEMI,
    2610041, '2022/05/23', '09/05/2022',
    2618012, '2022/09/02', '09/02/2022',
    2641012, '2022/07/25', '08/29/2022')

testframe %>% mutate(
  D.LastPV = as.IDate(D.LastPV, "%Y/%m/%d"),
  D.LastCEMI = as.IDate(D.LastCEMI, "%m/%d/%Y")
)

给出:

># A tibble: 3 × 3
   SUBJID D.LastPV   D.LastCEMI
    <dbl> <date>     <date>    
1 2610041 2022-05-23 2022-09-05
2 2618012 2022-09-02 2022-09-02
3 2641012 2022-07-25 2022-08-29

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2015-04-04
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多