【发布时间】: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.Date和Distribution.Date是factor向量。它们的类型是integer,但它们实际上是对字符串的编码。class(newdata$Distribution.Date)的输出是什么?查看?factor了解factor是什么,并检查read.table的stringsAsFactors参数及其相关函数。 -
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))