This is a very quick question, I also asked a more complicated one in this link
In this link, Roland kindly gave a very elegant solution to my problems. But I want to figure out why my solution doesn't work.
DT <- data.table(UID = paste0("UID",rep(1:5,each=2),
date = as.IDate("2012-01-01","2012-01-02","2012-01-03","2012-01-04","2012-01-05","2012-01-06","2012-02-01","2012-02-02","2012-02-03","2012-02-04"),
value = c(1:10)))
This is the fake data set.
Now I want to create a new variable called Val2 based on the following condition:
** Whether the date of each row is no "2012-01-02","2012-02-02".
To achieve this, I tried this
1. f <- function(x){
test <- ifelse(x %in% as.Date(c("2012-01-02","2012-02-02")) , TRUE, FALSE)
return(test)
}
DT[,Val2:= f(date)]
But all Val2 are true, which is obviously wrong.
As Roland pointed in my previous problem, I tried another one.
DT[, Val2:= sapply(date, function(x) x %in% as.Date(c("2012-01-02","2012-02-02")))]
Still not working with the following error:
Error in as.Date.default(x, ...) : do not know how to convert 'x' to class ate?
Also I tried to modified based on the error hints, but all failed.
Could you offer some suggestions? Thanks!