If DF is the data frame in your post then turn it into a "zoo"
object, convert that to class "ts" and then back to "zoo"
replacing
NA's with zero:
library(zoo)
z <- zoo(DF$Freq, as.yearmon(as.Date(paste(DF$Var1, "01", sep =
"-"))))
zz <- as.zoo(as.ts(z))
zz[is.na(zz)] <- 0
Suggest you read the zoo vignette:
vignette("zoo")
On 11/26/06, antonio rodriguez <antonio.raju at gmail.com>
wrote:> Hi,
>
> I have a list of 20 elements, each of them of variable length and with a
> structure like this:
>
> lasker[[1]][1:10,]
>
> Var1 Freq
> 1 1988-02 3
> 2 1988-03 1
> 3 1988-04 1
> 4 1988-05 2
> 5 1988-06 3
> 6 1988-07 1
> 7 1988-08 1
> 8 1988-09 1
> 9 1989-03 1
> 10 1989-04 1
>
> How do I can insert in this list:
>
> 1988-01 0
> 1988-10 0
> 1988-11 0
> 1988-12 0
> 1989-01 0
> 1989-02 0
>
> It's to say, the appropiate missing Year-Month row equal 0
>
> Antonio
>
> PD: follows dput output of lasker[[1]]
>
> dput(lasker[[1]],control="all")
> structure(list(Var1 = structure(as.integer(c(1, 2, 3, 4, 5, 6,
> 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
> 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
> 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
> 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
> 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
> 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
> 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
> 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
> 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
> 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153,
> 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166,
> 167, 168, 169, 170)), .Label = c("1988-02", "1988-03",
"1988-04",
> "1988-05", "1988-06", "1988-07",
"1988-08", "1988-09", "1989-03",
> "1989-04", "1989-05", "1989-07",
"1989-08", "1989-09", "1989-11",
> "1990-01", "1990-02", "1990-03",
"1990-04", "1990-05", "1990-06",
> "1990-07", "1990-08", "1990-10",
"1990-11", "1991-01", "1991-03",
> "1991-04", "1991-05", "1991-06",
"1991-09", "1991-11", "1991-12",
> "1992-01", "1992-03", "1992-04",
"1992-05", "1992-06", "1992-07",
> "1992-08", "1992-09", "1992-10",
"1992-11", "1992-12", "1993-01",
> "1993-02", "1993-03", "1993-04",
"1993-05", "1993-07", "1993-08",
> "1993-09", "1993-10", "1993-12",
"1994-02", "1994-03", "1994-04",
> "1994-05", "1994-07", "1994-08",
"1994-09", "1994-10", "1994-11",
> "1994-12", "1995-04", "1995-05",
"1995-06", "1995-07", "1995-08",
> "1995-09", "1995-10", "1995-11",
"1996-02", "1996-04", "1996-05",
> "1996-06", "1996-07", "1996-08",
"1996-09", "1996-11", "1996-12",
> "1997-01", "1997-02", "1997-04",
"1997-06", "1997-07", "1997-08",
> "1997-09", "1997-10", "1998-01",
"1998-02", "1998-04", "1998-05",
> "1998-06", "1998-08", "1998-09",
"1998-10", "1998-11", "1998-12",
> "1999-03", "1999-05", "1999-06",
"1999-07", "1999-08", "1999-09",
> "1999-10", "1999-11", "1999-12",
"2000-01", "2000-02", "2000-05",
> "2000-06", "2000-07", "2000-08",
"2000-09", "2000-10", "2000-11",
> "2000-12", "2001-02", "2001-03",
"2001-04", "2001-05", "2001-07",
> "2001-08", "2001-09", "2001-10",
"2001-11", "2001-12", "2002-01",
> "2002-02", "2002-04", "2002-05",
"2002-06", "2002-08", "2002-09",
> "2002-10", "2002-11", "2002-12",
"2003-01", "2003-02", "2003-03",
> "2003-04", "2003-05", "2003-06",
"2003-07", "2003-08", "2003-09",
> "2003-12", "2004-01", "2004-02",
"2004-03", "2004-04", "2004-05",
> "2004-06", "2004-07", "2004-08",
"2004-09", "2004-10", "2004-11",
> "2004-12", "2005-01", "2005-02",
"2005-03", "2005-04", "2005-06",
> "2005-07", "2005-08", "2005-09",
"2005-10", "2005-12"), class = "factor"),
> Freq = as.integer(c(3, 1, 1, 2, 3, 1, 1, 1, 1, 1, 2, 1, 2,
> 1, 1, 1, 2, 1, 1, 1, 3, 2, 1, 1, 3, 1, 1, 2, 1, 2, 1, 2,
> 1, 1, 2, 2, 3, 3, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2,
> 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1, 2,
> 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 1,
> 1, 1, 1, 1, 2, 3, 2, 4, 2, 2, 1, 3, 1, 3, 2, 1, 1, 2, 1,
> 2, 1, 1, 2, 2, 2, 2, 3, 1, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1,
> 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 2,
> 2, 1, 2, 1, 2, 3, 3, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2,
> 1, 2, 2, 1, 2))), .Names = c("Var1", "Freq"),
row.names > as.integer(c(NA,
> 170)), class = "data.frame")
>
> ______________________________________________
> R-help at 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.
>