【问题标题】:Is there an R function that converts dates from integer to actual dates?是否有将日期从整数转换为实际日期的 R 函数?
【发布时间】:2019-10-18 07:02:51
【问题描述】:

我正在尝试执行以下命令来改变新列。如果 Lease.End.date 为空,则应替换为 Distribution date else 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
.
.
.

【问题讨论】:

  • 不清楚你有什么问题。你的日期打印到控制台很好。
  • 你能把dput(newdata)的输出贴出来吗?
  • Lease.End.DateDistribution.Datefactor 向量。它们的类型是integer,但它们实际上是对字符串的编码。 class(newdata$Distribution.Date) 的输出是什么?查看?factor 了解factor 是什么,并检查read.tablestringsAsFactors 参数及其相关函数。
  • class(newdata$Distribution.Date)factor
  • dput(newdata) 有输出 structure(list(FI = c(3L, 3L, 3L, 3L,.....), Lease.End.Date = structure(c(34L, 1L, 1L, 27L, 31L, 7L, 8L, 9L...), Distribution.Date = structure(c(1L, 1L, 1L, 1L, 1L....), .Label = c("13-Aug-19", "16-Aug-19"), class = "factor"), correctdate = c(34L, 1L, 1L, 27L, 31L, 7L, 8L, 9L...) ), class = "data.frame", row.names = c(NA, -70L))

标签: r date dplyr


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多