R automatic names modification
I stumbled upon this weird behavior in R:
> a = 5
> names(a) <- "bar"
> b = c(foo = a)
> names(b)
[1] "foo.bar"
Why do the names get concatenated/stacked?
I found this c(a=b)
synt开发者_StackOverflowax in a script, but I couldn't find documentation about it. Is there any documentation for that?
Why do the names get concatenated/stacked?
Because it preserves all the name information that was present before the concatenation. If you don't like it, use unname
.
I found this c(a=b) syntax in a script, but I couldn't find documentation about it. Is there any documentation for that?
Some of the examples on the ?c
page demonstrate c(name = value)
behaviour, but there isn't much more to it than that. You might also want to look at ?names
.
It might also be instructive to see what happens if a
is a vector; in this case if foo=a
just redefined the name, all elements of the vector would end up with the same name. Instead, as in the following example, the four elements end up with unique names, which can be nice.
> a <- c(A=1, B=2)
> b <- c(A=3, B=4)
> c(a=a, b=b)
a.A a.B b.A b.B
1 2 3 4
精彩评论