2017-04-04 15 views
0

私はDFを連続した日付にして、次のようなNAも次のように言います。[1]は常に日付を持ちます。ループに日付を記入してください。

先行するNAの連続する日付を記入したいと思います。

私が持っている: "2016年1月1日"、 "2016年1月2日"、 "2016年1月3日"、DF内のNA、NA

期待される成果は次のようになります。 「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

if文は、真または偽の場合、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) 
    } 
} 
関連する問題