I am using a for-loop to go through a list of data frames. Inside the loop I filter each dataset (which include a date column) and then the output is used for calculations (including an ifelse loop). My problem is that sometime the output of the filter has no rows (empty dataset) and then it stopped the for-loop with the error
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
here is some code to reproduce the error message.
data1 <- data.frame(Date = as.Date("2016-01-01"), Filter1= 6)
data2 <- data.frame(Date = as.Date("2016-01-01"), Filter1= 3)
anomalies <- subset(data1, Filter1 <= 3) #won't work with the loop
anomalies <- subset(data2, Filter1 <= 3) #work with the loop
if(is.data.frame(anomalies) & nrow(anomalies)==0) {
anomalies[1, ] <- 0
} else{
anomalies <- anomalies
}
data2 will work but if I run data1 I get
Error in as.Date.numeric(value) : 'origin' must be supplied
I tried to format the date before and inside the ifelse loop but no success. I am aware of several questions on ifelse and date on SO but none deals with empty data frames. Any help on how to fix (or an alternative) would be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire