[R] Extending a vector to length n

Bert Gunter gunter.berton at gene.com
Thu Apr 16 20:24:52 CEST 2009


Folks:

Not to be picky, but depending on exactly what's meant by "attributes," I
think it's impossible:

> x <- 1:6
> attr(x,"length") <- length(x)
> xx <- rep(x,length=10)
> xx[7:10] <- NA
> attr(xx,"length")
NULL
> attr(x,"length")
[1] 6 

So "attributes" aren't preserved. The whole point of object orientation and
method dispatch is to deal with this issue in a coherent way.

Cheers,
Bert



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of hadley wickham
Sent: Thursday, April 16, 2009 10:50 AM
To: Raubertas, Richard
Cc: r-help
Subject: Re: [R] Extending a vector to length n

Great idea - that's a little faster than my previous approach of
setting length() and then re-adding the attributes.  Thanks!

Hadley

On Thu, Apr 16, 2009 at 12:16 PM, Raubertas, Richard
<richard_raubertas at merck.com> wrote:
> 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-bounces at r-project.org
>> [mailto:r-help-bounces at 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 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.
>>
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
> New Jersey, USA 08889), and/or its affiliates (which may be known
> outside the United States as Merck Frosst, Merck Sharp & Dohme or
> MSD and in Japan, as Banyu - direct contact information for affiliates is
> available at http://www.merck.com/contact/contacts.html) that may be
> confidential, proprietary copyrighted and/or legally privileged. It is
> intended solely for the use of the individual or entity named on this
> message. If you are not the intended recipient, and have received this
> message in error, please notify us immediately by reply e-mail and
> then delete it from your system.
>
>



-- 
http://had.co.nz/

______________________________________________
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.



More information about the R-help mailing list