Re: [R] Problems replicating rows and associated increasing index

From: jim holtman <jholtman_at_gmail.com>
Date: Thu 18 Jan 2007 - 15:32:45 GMT

Try this. It does a small sample with 15 weeks and only 10 patients, but the algorithm is easily changed for 52.

require(zoo) # need the 'zoo' library for 'na.locf' # create test data
visits <- data.frame(id=rep(1:10, each=10),value=runif(100), week=sample(1:15,100,TRUE))
# removed duplicated values
visits <- visits[!duplicated(paste(visits$id, visits$week)),] # now do the algorithm
full <- by(visits, visits$id, function(x){

    # fill to 15 weeks and add a baseline (0)     x <- rbind(x, data.frame(id=x$id[1], value=runif(1), week=0))     # determine which weeks are missing
    missing <- setdiff(1:15, x$week) # change to 52 for your case     # add missing weeks
    x <- rbind(x, data.frame(id=x$id[1], value=NA, week=missing))     # sort by week
    x <- x[order(x$week),]
    # replace 'value' with previous good value     x$value <- na.locf(x$value)
    x
})
# create new data frame
(full <- do.call(rbind, full))

On 1/18/07, Niccolò Bassani <biostatistica@gmail.com> wrote:
>
> I have a dataset like this:
>
> ID value week of visit
> 1 4.2 0 (baseline)
> 1 2.5 6
> 1 4.1 10
> 1 3.0 12
> .
> .
> .
>
> And so on for more than 11000 records (more than 1000 subjects). I need to
> add rows in order to have 52 records per patients (corresponding to the
> 52
> weeks of the year); that is that weeks 1-2-3-4-5 will have the same
> content
> of baseline one, except for the column "week of visit". The fact is that
> each of the rows can have a very different timing, so the only thing I can
> do is creating a vector giving the number of replications needed for each
> row, but how can I practically replicate them increasing the index ok the
> week?
> thanks
> niccolò
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>
>
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]


______________________________________________ R-help@stat.math.ethz.ch 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.

Received on Fri Jan 19 02:50:01 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 18 Jan 2007 - 16:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.