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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Jorge Cimentada
Hi everyone,

I was hoping someone would explain why this doesn't work.

c("a" = "b") # named character vector
class("a") # character
class("b") # character

c(names(mtcars)[1] = names(mtcars[2]) # error
class(names(mtcars)[1]) # character
class(names(mtcars)[2]) # character


Thanks,
Jorge Cimentada

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Error creating named character vectors from column names in data frame.

Jorge Cimentada
I'm sorry, there was a typo. The result is still the same:

c("a" = "b") # named character vector
class("a") # character
class("b") # character

c(names(mtcars)[1] = names(mtcars)[2]) # error
class(names(mtcars)[1]) # character
class(names(mtcars)[2]) # character

Thanks,
Jorge Cimentada

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Error creating named character vectors from column names in data frame.

Bert Gunter-2
In reply to this post by Jorge Cimentada
I think you have R semantics confused. What do you think

c("a" = "b")

means?

Note that:

> c("a" = "b")
  a
"b"
> a
Error: object 'a' not found


And of course:

class("a") # character
class("b") # character

but this has *nothing* to do with the line immediately above it.

Have you gone through any  basic R tutorials? There are many good ones
on the web.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Oct 7, 2016 at 9:06 AM, Jorge Cimentada <[hidden email]> wrote:

> Hi everyone,
>
> I was hoping someone would explain why this doesn't work.
>
> c("a" = "b") # named character vector
> class("a") # character
> class("b") # character
>
> c(names(mtcars)[1] = names(mtcars[2]) # error
> class(names(mtcars)[1]) # character
> class(names(mtcars)[2]) # character
>
>
> Thanks,
> Jorge Cimentada
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Error creating named character vectors from column names in data frame.

Jorge Cimentada
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. 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")

That was my initial question.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Error creating named character vectors from column names in data frame.

Duncan Murdoch-2
On 07/10/2016 1:44 PM, Jorge Cimentada 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. 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")
>
> That was my initial question.

It's a parser issue.  The parser is looking for a name before the equal
sign; it finds a string literal, and just to be nice, treats it as a
name.  But it never evaluates it as an expression; evaluation happens later.

The parser would be simpler (and apparently less confusing) if it just
refused to accept "a" where a name is needed, but at the time this was
written, there was no other way to express a name that was syntactically
a name, e.g. "bad name".  Later the `bad name` notation was added, and
it makes more sense to use that.

If you really want the result you expected, you can get it this way:

structure("b", names = names(df)[1])

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Error creating named character vectors from column names in data frame.

David Winsemius
In reply to this post by Jorge Cimentada

> On Oct 7, 2016, at 10:44 AM, Jorge Cimentada <[hidden email]> 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.


--
David

> 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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.