简体   繁体   English

将非日期格式的输出格式化为日期对象

[英]formatting the output which is not in a date format to a date object

Hi I am working on a dataset as shown below. 嗨,我正在处理数据集,如下所示。 I am trying to run the dmy function on date columns and return dates else return whatever is present in the data. 我试图在日期列上运行dmy函数,并返回日期,否则返回数据中存在的任何内容。 How do I get the date output as a Date and not as some long number. 如何将日期输出作为日期而不是一些长数字。 Thanks 谢谢

> dataF[1:5, 1:5]
 ID_NO     DTTM     DATE_YEAR  TERM    ISSUE_DT
1 1478   4-Sep-06     2006      27     2006-09-14
2 5032  27-Oct-06     2006      36     2006-11-02
3 3192   8-Jan-07     2007      10     2007-01-08
4 3978  28-Jan-07     2007      20     2007-01-31
5 6617   5-Jan-07     2007      10     2007-01-05
> 

> lapply(dataF[1:5, 1:5], function(x){ifelse(is.na(dmy(x)), x, dmy(x))})
$ID_NO
[1] "1478" "5032" "3192" "3978" "6617"

$DTTM
[1] 1157328000 1161907200 1168214400 1169942400 1167955200

$DATE_YEAR
[1] 2006 2006 2007 2007 2007

$TERM
[1] 27 36 10 20 10

$ISSUE_DT
[1] 1158192000 1162425600 1168214400 1170201600 1167955200

I suppose it's ifelse that returns the numeric vector. 我想是ifelse返回数字向量。 The documentation says that 该文件说

ifelse returns a value with the same shape as test ifelse返回与test形状相同的值

so that's why (presumably) a POSIXct vector is forced to be numeric. 因此,这就是为什么(大概)将POSIXct向量强制为数字的原因。 This workaround may help: 此解决方法可能会帮助:

lapply(df, function(x){
    y <- dmy(x)
    if (all(is.na(y))) return(x)
    y
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM