The following approach works for both of your examples: xx <- rep(x, length.out=n) xx[m:n] <- NA
Thus: > n <- 2 > aa <- rep(a, length.out=n) > aa[(length(a)+1):n] <- NA > aa [1] "2008-01-01" NA > bb <- rep(b, length.out=n) > bb[(length(b)+1):n] <- NA > bb [1] a <NA> Levels: a > R. Raubertas Merck & Co > -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of hadley wickham > Sent: Wednesday, April 15, 2009 10:55 AM > To: r-help > Subject: [R] Extending a vector to length n > > In general, how can I increase a vector of length m (< n) to length n > by padding it with m - n missing values, without losing attributes? > The two approaches I've tried, using length<- and adding missings with > c, do not work in general: > > > a <- as.Date("2008-01-01") > > c(a, NA) > [1] "2008-01-01" NA > > length(a) <- 2 > > a > [1] 13879 NA > > > > b <- factor("a") > > c(b, NA) > [1] 1 NA > > length(b) <- 2 > > b > [1] a <NA> > Levels: a > > Hadley > > > > -- > http://had.co.nz/ > > ______________________________________________ > 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. > Notice: This e-mail message, together with any attachme...{{dropped:12}} ______________________________________________ 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.