2

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!

4
  • 1
    Your code to build DT is malformed. Please fix it. Commented Jan 16, 2014 at 2:49
  • Thanks Matthew:) Already fixed Commented Jan 16, 2014 at 2:55
  • I beg to differ. Paste from the question into your R interpreter. Commented Jan 16, 2014 at 2:56
  • @MatthewLundberg I just copied the code from Rstudio, but when I copied back it doesn't work neither..Now hope the code is OK. Thanks Commented Jan 16, 2014 at 3:08

1 Answer 1

3

Compare to an object of the same class (IDate). You also do not need to use ifelse to here, as it is just returning the value of %in%.

DT[,val2 := date %in% as.IDate(c("2012-01-02","2012-02-02"))]
> DT
     UID       date value  val2
 1: UID1 2012-01-01     1 FALSE
 2: UID1 2012-01-02     2  TRUE
 3: UID2 2012-01-03     3 FALSE
 4: UID2 2012-01-04     4 FALSE
 5: UID3 2012-01-05     5 FALSE
 6: UID3 2012-01-06     6 FALSE
 7: UID4 2012-02-01     7 FALSE
 8: UID4 2012-02-02     8  TRUE
 9: UID5 2012-02-03     9 FALSE
10: UID5 2012-02-04    10 FALSE

DT, prior to the addition of val2:

structure(list(UID = c("UID1", "UID1", "UID2", "UID2", "UID3", 
"UID3", "UID4", "UID4", "UID5", "UID5"), date = structure(c(15340L, 
15341L, 15342L, 15343L, 15344L, 15345L, 15371L, 15372L, 15373L, 
15374L), class = c("IDate", "Date")), value = 1:10), .Names = c("UID", 
"date", "value"), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.