[R] Error creating named character vectors from column names in data frame.

David Winsemius dwinsemius at comcast.net
Sat Oct 8 00:26:43 CEST 2016

> On Oct 7, 2016, at 10:44 AM, Jorge Cimentada <cimentadaj at gmail.com> wrote:
> Hi Bert,
> Yes, I'm aware of the difference between a and "a" but in terms of object
> classes I don't see the difference between "a" and names(mtcars)[1].
> They're both strings.

They are not both "strings". One is a character value and the other is an expression that requires two different function evaluations. Inside the call to `c()`, the expressions on either side of an "="-sign are handled differently than they are outside a matching set of function parentheses. I _think_ they are parsed as 'alists', and teh which the ?alist page says the alist()-function does not evaluate the LHS of a pair being formed by the "="-operator. I was, however, not able to find descriptions of this in either The R Language Definition or R Internals.

I see Duncan has pointed out that "a" is converted to an R symbol/name as a "courtesy". There is some irony in the fact that the `names` function does not return R names, which are language objects with mode name.


> However, for creating a named character vector, this
> works:
> c("a" = "b)
> But this doesn't
> c(names(mtcars)[1] = "b")

> For example:
> df <- data.frame("a" = 1:5)
> c("a" = "b")
> c(names(df)[1] = "b") # error
> But
> identical(names(df)[1], "a")

The `identical` function evaluates both its arguments.

> That was my initial question.
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA

More information about the R-help mailing list