Hello, Maybe there are simpler ways of doing it, but try the following.
sp <- lapply(split(dat, dat$id), function(.s){ i <- min(which(.s$stat == 0), which(.s$g == 1)) .s$d <- .s$stop[i] .s[-1][row(.s[-1]) >= i] <- NA .s }) dat3 <- do.call(rbind, sp) rownames(dat3) <- seq_len(nrow(dat3)) all.equal(dat2, dat3) # only names are different Hope this helps, Rui Barradas Em 17-09-2012 19:32, john james escreveu: > Dear R users, > > I have the following problems. My dataset (dat) is as follows: > > a <- c(1,2,3) > id <- rep(a, c(3,2,3)) > stat <- c(1,1,0,1,0,1,1,1) > g <- c(0,0,0,0,0,0,1,0) > stop <- c(1,2,4,2,4,1,1.5,3) > dat <- data.frame(id,stat,g,stop) > > I want to creat a new dataset (dat2) with missing values > such that when either g = =1 or stat = =0, the remaining rows for an > individual subject is set to NA by using a new variable d (that states > the exact time this > happened from the stop variable). By this I mean dat2 that looks like, > > id <- rep(a, c(3,2,3)) > sta2<- c(1,1,NA,1,NA,1,NA,NA) > g2<- c(0,0,NA,0,NA,0,NA,NA) > stop2 <- c(1,2,NA,2,NA,1,NA,NA) > d <- c(4,4,NA,4,NA,1.5,NA,NA) > > dat2 <- data.frame(id=id, stat2=sta2, g2=g2,stop2=stop2,d=d). > > Thank you very much! > > John > [[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.