Niccolò Bassani
2007-Jan-18 14:45 UTC
[R] Problems replicating rows and associated increasing index
? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile Url: https://stat.ethz.ch/pipermail/r-help/attachments/20070118/911a5d2a/attachment.pl
jim holtman
2007-Jan-18 15:32 UTC
[R] Problems replicating rows and associated increasing index
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]]