Hi:

Here are a few ways:

(1) ave():
transform(dat, dropout.time = ave(y, id, FUN = function(x) sum(!is.na(x))))

(2) same as (1) without the transform() statement:
dat$dropout.time <- ave(y, id, FUN = function(x) sum(!is.na(x)))
dat

(3) ddply() in the plyr package:

library(plyr)
snisna <- function(x) sum(!is.na(x))
ddply(dat, .(id), transform, dropout.time = snisna(y))

HTH,
Dennis

On Sun, May 30, 2010 at 7:25 PM, john james <dnt...@yahoo.com> wrote:

> Dear R users,
>
> Please assist me with the following problem. I have a dataset that looks
> like the following:
>
> dat<-data.frame(
>   'id'=rep(c(1,2,3),each=3),
>   'time'=rep(c(1,2,3),3),
>   'y'= c(2,2,NA,2,NA,NA,2,5,7)
> )
>
>
> I wish to create a variable for dropout time in dataframe 'dat' such that
> the dropout time is the time to drop out by the subject as follows:
>
>
> dat<-data.frame(
>   'id'=rep(c(1,2,3),each=3),
>   'time'=rep(c(1,2,3),3),
>   'y'= c(2,2,NA,2,NA,NA,2,5,7),
>   'dropout time'=c(2,2,2,1,1,1,3,3,3)
> )
>
> Any help will be appreciated. Many thanks in advance.
>
> james
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to