Skip to content

Filling in Gapped Interval of a Data Frame As Series

3 messages · Gundala Viswanath, Dimitris Rizopoulos, Gabor Grothendieck

#
Dear all,

I have a data frame that looks like this:
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:
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
#
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:

  
    
#
Try this:
+   -6      20.2
+   -1      1.5
+    2      3.00
+    3      78.8
+    5      33.2
+    6      44.5
+    7     5.00"
-8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4
100.0   0.0  20.2   0.0   0.0   0.0   0.0   1.5   0.0   0.0   3.0  78.8   0.0
    5     6     7
 33.2  44.5   5.0


If you want to create a data frame from that try this:

newDF <- data.frame(Time = time(zz), Value = coredata(zz))

although you might be best off to just leave it as a zoo object
so that you can make use of other zoo methods too.
See the three zoo vignettes (PDF documents) for more info.
On Sun, Apr 5, 2009 at 3:22 AM, Gundala Viswanath <gundalav at gmail.com> wrote: