[R] Filling in Gapped Interval of a Data Frame As Series

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sun Apr 5 10:12:32 CEST 2009


one way is the following:

dat. <- data.frame(V1 = min(dat$V1):max(dat$V1), V2 = 0)
newdat <- merge(dat, dat., by = "V1", all.y = TRUE, suffixes = c("", "."))
newdat$V2[na.ind] <- newdat$V2.[na.ind <- is.na(newdat$V2)]
newdat[-3]


I hope it helps.

Best,
Dimitris


Gundala Viswanath wrote:
> Dear all,
> 
> I have a data frame that looks like this:
> 
>> dat
>     V1     V2
> 1  -8      100
> 2  -6      20.2
> 3  -1      1.5
> 4   2      3.00
> 5   3      78.8
> 6   5      33.2
> 7   6      44.5
> 8   7     5.00
> 
> Now I want to fill the V1 column  in series (note that it is gapped),
> with the corresponding value in V2 as 0.
> 
> So in the end we would like to have:
> 
>> newdat
>     V1     V2
> 1   -8      100
> 2   -6      20.2
> 3   -7      0
> 4   -6      0
> 5   -5      0
> 6    -4     0
> 7    -3     0
> 8    -2     0
> 9    -1     1.5
> 10    0     0
> 11    1     0
> 12   2      3.00
> 13   3      78.8
> 14   4      0
> 15   5      33.2
> 16   6      44.5
> 17   7      5.00
> 
> Is there a way to do it in R?
> - Gundala Viswanath
> Jakarta - Indonesia
> 
> ______________________________________________
> R-help at 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list