我有一个DF连续日期,其中有一些NA跟随,值得一提的是[1]将始终有一个日期。
我希望连续日期填写NA的程序。
我有:“2016-01-01”,“2016-01-02”,“2016-01-03”,NA,NA
DF内的预期结果将是: “2016-01-01”,“2016-01-02”,“2016-01-03”,“2016-01-04”,“2016-01-05”
我试图使用for循环但我在这方面非常业余:
Date <-as.Date(c("2016-01-01", "2016-01-02", "2016-01-03", NA, NA))
DF <- as.data.frame(Date)
i <- which.max(is.na(DF$Date))
for (i in which.max(is.na(DF$Date)):max(length(DF$Date))){
if(is.na(DF$Date)){
DF$Date[i]= as.Date(max(DF$Date, na.rm=TRUE) + 1)
}
}
它返回:
警告讯息:
1: In if (is.na(DF$Date)) { :
the condition has length > 1 and only the first element will be used
2: In if (is.na(DF$Date)) { :
the condition has length > 1 and only the first element will be used
答案 0 :(得分:0)
我自己发现,if语句不是单个值,如果是真或假NA,它列出整个DF。
Date <-as.Date(c("2016-01-01", "2016-01-02", "2016-01-03", NA, NA))
DF <- as.data.frame(Date)
i <- which.max(is.na(DF$Date))
for (i in which.max(is.na(DF$Date)):max(length(DF$Date))){
if(which.max(is.na(DF$Date))){
DF$Date[i]= as.Date(max(DF$Date, na.rm=TRUE) + 1)
}
}