William Dunlap
2009-Sep-18 22:34 UTC
[Rd] what should names(x) be padded with when length(x) is increased?
R version 2.10.0 Under development (unstable) (2009-09-08 r49628)
Here are two somewhat related questions.
First, when we attach a too short names vector to a vector
the names vector is padded with NA's to the length of the
main vector: E.g.,
> x<-1:3
> names(x)<-c("One","Two")
> str(x)
Named int [1:3] 1 2 3
- attr(*, "names")= chr [1:3] "One" "Two" NA
I expected this length mismatch to cause an error, but I am
biassed by my long usage of S and S+. Is the extension by
padding with NA what is wanted?
Second, if we extend a named vector with length<- the main
vector is padded with NA's but the names are padded with
empty strings. Should those be NA's?
> x<-1:3
> names(x)<-c("One","Two")
> length(x)<-4
> str(x)
Named int [1:4] 1 2 3 NA
- attr(*, "names")= chr [1:4] "One" "Two" NA
""
The names "" and NA act alike in that subscript by either
returns structure(NA, names=NA_character_), but they
print differently
> x
One Two <NA>
1 2 3 NA
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
Gabor Grothendieck
2009-Sep-18 22:44 UTC
[Rd] what should names(x) be padded with when length(x) is increased?
The graphviz site, itself, lists resources/alternatives here: http://www.graphviz.org/Resources.php On Fri, Sep 18, 2009 at 6:34 PM, William Dunlap <wdunlap at tibco.com> wrote:> R version 2.10.0 Under development (unstable) (2009-09-08 r49628) > > Here are two somewhat related questions. > > First, when we attach a too short names vector to a vector > the names vector is padded with NA's to the length of the > main vector: ?E.g., > ? > x<-1:3 > ? > names(x)<-c("One","Two") > ? > str(x) > ? ?Named int [1:3] 1 2 3 > ? ?- attr(*, "names")= chr [1:3] "One" "Two" NA > I expected this length mismatch to cause an error, but I am > biassed by my long usage of S and S+. ?Is the extension by > padding with NA what is wanted? > > Second, if we extend a named vector with length<- the main > vector is padded with NA's but the names are padded with > empty strings. ?Should those be NA's? > > ? > x<-1:3 > ? > names(x)<-c("One","Two") > ? > length(x)<-4 > ? > str(x) > ? ?Named int [1:4] 1 2 3 NA > ? ?- attr(*, "names")= chr [1:4] "One" "Two" NA "" > > The names "" and NA act alike in that subscript by either > returns structure(NA, names=NA_character_), but they > print differently > ? > x > ? ?One ?Two <NA> > ? ? 1 ? ?2 ? ?3 ? NA > > > Bill Dunlap > TIBCO Software Inc - Spotfire Division > wdunlap tibco.com > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >