[R] Using lapply in R data table

Ista Zahn istazahn at gmail.com
Mon Sep 26 19:05:51 CEST 2016


Hi Frank,

lapply(DT) iterates over each column. That doesn't seem to be what you want.

There are probably better ways, but here is one approach.

DT[, exposure := vector(mode = "numeric", length = .N)]
DT[fini < as.Date("2006-01-01"), exposure := 1]
DT[fini >= as.Date("2006-01-01") & fini <= as.Date("2006-06-30"),
      exposure := difftime(as.Date("2007-01-01"), fini, units="days")/365.25]
DT[fini >= as.Date("2006-07-01"), exposure := 0.5]

Best,
Ista

On Mon, Sep 26, 2016 at 11:28 AM, Frank S. <f_j_rod at hotmail.com> wrote:
> Dear all,
>
> I have a R data table like this:
>
> DT <- data.table(
>   id = rep(c(2, 5, 7), c(3, 2, 2)),
>   fini = rep(as.Date(c('2005-04-20', '2006-02-19', '2006-10-08')), c(3, 2, 2)),
>   group = rep(c("A", "B", "A"), c(3, 2, 2))  )
>
>
> I want to construct a new variable "exposure" defined as follows:
>
> 1) If "fini" earlier than 2006-01-01 --> "exposure" = 1
> 2) If "fini" in [2006-01-01, 2006-06-30] --> "exposure" = "2007-01-01" - "fini"
> 3) If "fini" in [2006-07-01, 2006-12-31] --> "exposure" = 0.5
>
>
> So the desired output would be the following data table:
>
>    id                fini exposure group
> 1:  2 2005-04-20        1.00        A
> 2:  2 2005-04-20        1.00        A
> 3:  2 2005-04-20        1.00        A
> 4:  5 2006-02-19        0.87        B
> 5:  5 2006-02-19        0.87        B
> 6:  7 2006-10-08        0.50        A
> 7:  7 2006-10-08        0.50        A
>
>
> I have tried:
>
> DT <- DT[ , list(id, fini, exposure = 0, group)]
> DT.new <- lapply(DT, function(exposure){
>       exposure[fini < as.Date("2006-01-01")] <- 1   # 1st case
>       exposure[fini >= as.Date("2006-01-01") & fini <= as.Date("2006-06-30")] <- difftime(as.Date("2007-01-01"), fini, units="days")/365.25 # 2nd case
>     exposure[fini >= as.Date("2006-07-01") & fini <= as.Date("2006-12-31")] <- 0.5       # 3rd case
>       exposure  # return value
>   })
>
>
> But I get an error message.
>
> Thanks for any help!!
>
>
> Frank S.
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list