简体   繁体   中英

Updating Variable using functions in data.table?

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!

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM