Problems with SE, mutate_each_, and variables with spaces

31 views
Skip to first unread message

Stuart Luppescu

unread,
Mar 14, 2016, 7:19:29 PM3/14/16
to manip...@googlegroups.com
This has rather been driving me crazy. It should be straightforward but it's giving me lots of problems.
I have a data frame with variables that I want to summarize by two classification variables. The variables to summarize are

demo_vars <- c("Pct ELL", "Pct Free/Reduced Lunch", "Pct White", "Pct Afr-Am", "Pct Latino", "Pct Asian")

The code I'm trying to run is:

    the.var.m <- demo_by_schl %>%
    group_by(level, schl_id) %>%
        summarize_each_(funs(mean), demo_vars)

But when I try this I get this error message:

Error in parse(text = x) : <text>:1:5: unexpected symbol
1: Pct ELL
        ^

Is this because of the space in the variable name? If so, how do I get around it? If not, what else am I doing wrong?

TIA.
--
Stuart Luppescu -- pixbuf .at. gmail.com
I made up a new word: plagiarism.
  --jpuller

Brandon Hurr

unread,
Mar 14, 2016, 7:30:15 PM3/14/16
to Stuart Luppescu, manip...@googlegroups.com
I think it is very much because of the space and other non-allowed characters in the column names. I usually do:

names(df) <- make.names(names(df))

It should clean them up but will add loads of '.' To the names so your name vector will need these new names.

If that doesn't work, post a full example.

B
--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Hadley Wickham

unread,
Mar 15, 2016, 10:06:03 AM3/15/16
to Stuart Luppescu, manipulatr
Convert the string to names:

lapply(demo_vars, as.name)

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at https://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz
Reply all
Reply to author
Forward
0 new messages