0

I want to set certain dates as NA if those date from a certain year (in this example, 2015)

df <- structure(list(year = c(2014, 2014, 2014, 2014, 2014, 2015, 2015, 
                              2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016), 
                     date = structure(c(16229, 16236, 16243, 16250, 16257, 16600, 
                                        16607, 16614, 16621, 16628, 16964, 16971, 16978, 16985, 16992), class = "Date"), 
                     value = c(0.27, 0.37, 0.57, 0.91, 0.2, 0.9, 0.94, 0.66, 0.63, 0.06, 0.21, 0.18, 
                               0.69, 0.38, 0.77)), .Names = c("year", "date", "value"), 
                row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

So, I have tried using ifelse()but, it returns numeric values:

df$date2 <- ifelse(df$year==2015, NA_character_, df$date)

I have tried using if_else() from dplyr, but it seems it is more strict, in which the true and false argument must be of a same classs. Anyone know any specific function for this?

0 Answers0