On Nov 1, 2010, at 3:34 PM, blurg wrote:


I have a data set similar to the set below where 1 and 2 indicate test
results and 0 indicates time points in between where there are no test
results. I would like to allocate the time points leading up to a test
result with the value of the test result.

What I have:     What I want:
1                     1
0                     1
0                     1
0                     1
1                     1
0                     2
0                     2
2                     2
0                     1
0                     1
1                     1
0                     2
2                     2

I have attempted methods creating a data.frame of the the breaks/ changes in
of values to from 0 to 1 or to 2.
x<-c(0,2,0,1,0,0,0,0,1,0,1,0,0,0,2,1,0,0,0,2,0,0,0,1)
x1 <- which(diff(x) == 1)
x2 <- which(diff(x) == 2)

Not sure how long you longest run of zeros is but repeate applications of htis method n-such times will fill in in the backward direction:

> xna <- x
> xna[xna==0] <- NA
> xna[which(is.na(xna))] <- xna[which(is.na(xna))+1]
> xna
[1] 2 2 1 1 NA NA NA 1 1 1 1 NA NA 2 2 1 NA NA 2 2 NA NA 1 1
> xna[which(is.na(xna))] <- xna[which(is.na(xna))+1]
> xna
[1] 2 2 1 1 NA NA 1 1 1 1 1 NA 2 2 2 1 NA 2 2 2 NA 1 1 1
> xna[which(is.na(xna))] <- xna[which(is.na(xna))+1]
> xna[which(is.na(xna))] <- xna[which(is.na(xna))+1]
> xna[which(is.na(xna))] <- xna[which(is.na(xna))+1]
> xna
 [1] 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 1 2 2 2 2 1 1 1 1

I'm not sure that conversion to NA is needed. The indexing with which(x==0) and which(x==0+1 might work as well. Yep... that work's too:

> x
 [1] 0 2 0 1 0 0 0 0 1 0 1 0 0 0 2 1 0 0 0 2 0 0 0 1
> x[which(x==0)] <- x[which(x==0)+1]
> x
 [1] 2 2 1 1 0 0 0 1 1 1 1 0 0 2 2 1 0 0 2 2 0 0 1 1
> x[which(x==0)] <- x[which(x==0)+1]
> x[which(x==0)] <- x[which(x==0)+1]
> x[which(x==0)] <- x[which(x==0)+1]
> x
 [1] 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 1 2 2 2 2 1 1 1 1


--
David



What ever the solution, I can't be entered by hand due to the size of the dataset (>10 million and change). Any ideas? This is my first time posting to this forum and I am relatively new to R, so please don't flame me to
hard.  Desperate times call for desperate measures.  Thanks.
--


David Winsemius, MD
West Hartford, CT

______________________________________________
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