首页 > 解决方案 > 是否有将日期从整数转换为实际日期的 R 函数?

问题描述

我正在尝试执行以下命令来改变新列。如果 Lease.End.date 为空白,则应将其替换为 Distribution date 否则 Lease.End.Date。

我得到了正确日期列的不正确值

newdata <- data %>% select(FI, Lease.End.Date, Distribution.Date) %>% mutate(correctdate = ifelse(Lease.End.Date == "", 
                                                Distribution.Date,Lease.End.Date))
> typeof(newdata$correctdate)
[1] "integer"
> typeof(newdata$Lease.End.Date)
[1] "integer"
> typeof(newdata$Distribution.Date)
[1] "integer"  

> data

       FI Lease.End.Date Distribution.Date
1       3      31-Jul-25         13-Aug-19
2       3                        13-Aug-19
3       3                        13-Aug-19
4       3      31-Jan-19         13-Aug-19
5       3      31-Jan-24         13-Aug-19
6       3      13-Aug-20         13-Aug-19
7       3      13-Aug-21         13-Aug-19
8       3      13-Aug-22         13-Aug-19
9       3      13-Aug-23         13-Aug-19
10      3      13-Aug-24         13-Aug-19

> newdata
       FI Lease.End.Date Distribution.Date correctdate
1       3      31-Jul-25         13-Aug-19          34
2       3                        13-Aug-19           1
3       3                        13-Aug-19           1
4       3      31-Jan-19         13-Aug-19          27
5       3      31-Jan-24         13-Aug-19          31
6       3      13-Aug-20         13-Aug-19           7
7       3      13-Aug-21         13-Aug-19           8
8       3      13-Aug-22         13-Aug-19           9
9       3      13-Aug-23         13-Aug-19          10  


I want the correctdate column should have following values:

31-Jul-25
13-Aug-19
13-Aug-19
31-Jan-19
.
.
.

标签: rdatedplyr

解决方案


尝试这个:

library(lubridate)
library(dplyr)
data <- data.frame(FI = c(3,3,3, 3,3), Lease.End.Date = c("31-Jul-25", "", "", "31-Jan-19", "31-Jan-24"), Distribution.Date = c("13-Aug-19", "13-Aug-19", "13-Aug-19", "13-Aug-19", "13-Aug-19"))

# -------------------------------------------------------------------------
typeof(data$Lease.End.Date)                                                                             
#[1] "integer"
class(data$Lease.End.Date)
#[1] "factor"

typeof(data$Distribution.Date)                                               
#"integer"
class(data$Distribution.Date)                                               
#[1] "factor"

# -------------------------------------------------------------------------
# format the columns as date using lubridate date functions
# -------------------------------------------------------------------------

data$Distribution.Date <- dmy(data$Distribution.Date)

data$Lease.End.Date <- dmy(data$Lease.End.Date)

class(data$Distribution.Date)
#[1] "Date"
class(data$Lease.End.Date) 
#[1] "Date"

# -------------------------------------------------------------------------
# the ifelse
newdata <- data %>% 
  select(FI, Lease.End.Date, Distribution.Date) %>% 
  mutate(correctdate = as.Date(ifelse(is.na(Lease.End.Date) ,Distribution.Date, Lease.End.Date), origin="1970-01-01")) 
# -------------------------------------------------------------------------

# newdata
#   FI Lease.End.Date Distribution.Date correctdate
# 1  3     2025-07-31        2019-08-13  2025-07-31
# 2  3           <NA>        2019-08-13  2019-08-13
# 3  3           <NA>        2019-08-13  2019-08-13
# 4  3     2019-01-31        2019-08-13  2019-01-31
# 5  3     2024-01-31        2019-08-13  2024-01-31

推荐阅读