[R] Looping through data tables (or data frames) by removing previous individuals
Frank S.
f_j_rod at hotmail.com
Tue Oct 4 12:31:50 CEST 2016
Thank you very much Ista and Zahn!
Best,
Frank S.
________________________________
De: Charles C. Berry <ccberry at ucsd.edu>
Enviat el: dilluns, 3 d'octubre de 2016 21:38:05
Per a: Frank S.
A/c: r-help at r-project.org
Tema: Re: Looping through data tables (or data frames) by removing previous individuals
On Mon, 3 Oct 2016, Frank S. wrote:
> Dear R users,
>
>
[deleted]
> I want to get a list of "k" data tables (or data frames) so that each
> contains those individuals who for the first time are at least 65,
> looping on each of the dates of vector "v". Let's consider the following
> example with 5 individuals:
>
>
> dt <- data.table(
> id = 1:5,
> fborn = as.Date(c("1935-07-25", "1942-10-05", "1942-09-07", "1943-09-07", "1943-12-31")),
> sex = as.factor(rep(c(0, 1), c(2, 3)))
> )
>
> v <- seq(as.Date("2006-01-01"), as.Date("2009-01-01"), by ="year") # k=4
>
>
> I would expect to obtain k=4 data tables so that:
> dt_p1: contains id = 1 (he is for the first time at least 65 on date v[1])
> dt_p2: is NULL (no subject reach for the first time 65 on date v[2])
> dt_p3: contains id = 2 & id = 3 (they are for the first time at least 65 on v[3])
> dt_p4: contains id = 4 & id = 5 (they are for the first time at least 65 on v[4])
>
>
Here is a start (using a data.frame for dt):
> vp <- as.POSIXlt( c( as.Date("1000-01-01"), v ))
> vp$year <- vp$year-65
> dt.cut <- as.numeric(cut(as.POSIXlt(dt$fborn),vp))
> split(dt,factor(dt.cut, 1:length(v)))
$`1`
id fborn sex
1 1 1935-07-25 0
$`2`
[1] id fborn sex
<0 rows> (or 0-length row.names)
$`3`
id fborn sex
2 2 1942-10-05 0
3 3 1942-09-07 1
$`4`
id fborn sex
4 4 1943-09-07 1
5 5 1943-12-31 1
See
?as.POSIXlt
?cut.POSIXt
?split
HTH,
Chuck
[[alternative HTML version deleted]]
More information about the R-help
mailing list