将字符转换为日期,而无需在转换后将其中一些字符作为 NA

Convert characters to dates without having some of them as NAs after the convertion

提问人:firmo23 提问时间:11/15/2023 最后编辑:firmo23 更新时间:11/15/2023 访问量:35

问:

我有这些数据

    sg2<-structure(list(`Last prescription/or progression time (if progressed)` = c("23-11-2021", 
"28-09-2022", "45020", "45079", "23-05-2023"), time = c(3.682191781, 
9.008219178, 22.52054795, 20.02191781, 12.7890411), status = c(0, 
0, 0, 0, 0), `Number of bone lesions (<5 = “<5”, >=5 = “≥5”) (clear, multiple bone metastases ≥5 are defined)` = c(">=5", 
"<5", ">=5", ">=5", "<5")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

例如,我的一些日期数据显示为 44383,而其他日期则从我的 Excel 文件中正常加载。我尝试将它们全部转换为下面的日期,但随后我得到的日期为 NA

sg2$`Time of first prescription of denosumab (if enrolled in QL1206 and JMT, this time is the time of first use of XGEVA after leaving the group)` <- as.Date(as.numeric(sg2$`Time of first prescription of denosumab (if enrolled in QL1206 and JMT, this time is the time of first use of XGEVA after leaving the group)`), origin = "1899-12-30")
Warning message:
In as.Date(as.numeric(sg2$`Time of first prescription of denosumab (if enrolled in QL1206 and JMT, this time is the time of first use of XGEVA after leaving the group)`),  :
  NAs introduced by coercion
r 日期

评论


答:

1赞 jkatam 11/15/2023 #1

请尝试以下代码

sg2 %>% rename('col1' =1) %>% mutate(col11= ifelse(nchar(col1)==10, col1, NA),
                                               col11=as.Date(col11, tryFormats='%d-%m-%Y'),
                                               col12= ifelse(nchar(col1)==5, col1, NA),
                                               col12= as.Date(as.numeric(col12), origin='1970-01-01'),
                                               col1=coalesce(col11,col12)
) %>% select(-c(col11, col12))


# A tibble: 5 × 4
  col1        time status Number of bone lesions (<5 = “<5”, >=5 = “≥5”) (clear, multiple bone metastases ≥5 are defin…¹
  <date>     <dbl>  <dbl> <chr>                                                                                         
1 2021-11-23  3.68      0 >=5                                                                                           
2 2022-09-28  9.01      0 <5                                                                                            
3 2093-04-05 22.5       0 >=5                                                                                           
4 2093-06-03 20.0       0 >=5                                                                                           
5 2023-05-23 12.8       0 <5                                                                                            
# ℹ abbreviated name:
#   ¹​`Number of bone lesions (<5 = “<5”, >=5 = “≥5”) (clear, multiple bone metastases ≥5 are defined)`


评论

0赞 firmo23 11/15/2023
如果我想将它应用于具有单独日期的实际数据集的每一列,您能否进行调整?考虑到没有像我编辑的数据那样的第一列
0赞 jkatam 11/15/2023
是的,你可以,但我没有正确理解你的问题,所以除非你提供虚拟数据,否则可能不会提供代码
0赞 firmo23 11/15/2023
好的,对不起,我更新了上面的 SG2 数据集,因为这正是我的实际情况,所以请应用任何可能有效的解决方案
0赞 firmo23 11/15/2023
你检查过了吗?
0赞 jkatam 11/15/2023
更新了答案,请查收