arrange where the column name is in a variable

2,211 views
Skip to first unread message

Doug Mitarotonda

unread,
Sep 11, 2012, 11:06:11 AM9/11/12
to manip...@googlegroups.com
Hi everyone,
I came to this question in a similar fashion as my previous post on spaces in names with **ply, but this is slightly different because I can't get arrange to work where the column name to arrange by is stored in a variable regardless of spaces or not. 

> df <- data.frame(x=c(3,2,1), "y z" = c(12,6,9), check.names = FALSE)
> r <- "x"
> s <- "y z"

Using arrange, I am trying to produce the following that works with order:
> df[order(df[[r]]), ]
  x y z
3 1   9
2 2   6
1 3  12
> df[order(df[[s]]), ]
  x y z
2 2   6
3 1   9
1 3  12

It is clear that arrange does work with when using backticks with the name directly:
> library(plyr)
> arrange(df, `x`)
  x y z
1 1   9
2 2   6
3 3  12
> arrange(df, `y z`)
  x y z
1 2   6
2 1   9
3 3  12

but not when stored in a variable (note, I threw in my solution to the previous post on creating a .. function that has been working great for **ply calls)
> arrange(df, r)
Error: Length of ordering vectors don't match data frame size
> arrange(df, .(r))
Error: Length of ordering vectors don't match data frame size
> `..` <- function(x){return(paste0("`", x, "`"))}
> arrange(df, ..(r))
Error: Length of ordering vectors don't match data frame size

Does anyone have any suggestions on how to use arrange where the column is a variable name? I feel like I am missing something obvious here, so I apologize in advance if that is the case.

Best wishes,
Doug

Hadley Wickham

unread,
Sep 11, 2012, 11:51:51 AM9/11/12
to Doug Mitarotonda, manip...@googlegroups.com
Hi Doug,

Yup, that's how arrange works. If the column name is a variable, then
use regular subsetting + order (the same applies for subset,
summarise, and mutate)

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/manipulatr/-/7kaxP_y2sjAJ.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to
> manipulatr+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/manipulatr?hl=en.



--
RStudio / Rice University
http://had.co.nz/

Doug Mitarotonda

unread,
Sep 11, 2012, 12:20:36 PM9/11/12
to manip...@googlegroups.com, Doug Mitarotonda
Hey Hadley,
Thanks very much for the quick response.

Best wishes,
Doug

Doug Mitarotonda

unread,
Sep 11, 2012, 5:58:42 PM9/11/12
to manip...@googlegroups.com, Doug Mitarotonda
Hadley, am I correct that this is also true for ggplot - i.e., writing the specific variable name in backticks works, but not if the variable is stored in a variable?

Hadley Wickham

unread,
Sep 11, 2012, 6:01:28 PM9/11/12
to Doug Mitarotonda, manip...@googlegroups.com
That's right for aes. If you have the name of the variable stored in a string you can use aes_string. 

Hadley
To view this discussion on the web visit https://groups.google.com/d/msg/manipulatr/-/jw1QsY39f2sJ.

To post to this group, send email to manip...@googlegroups.com.
To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.

Doug Mitarotonda

unread,
Sep 11, 2012, 6:11:04 PM9/11/12
to manip...@googlegroups.com, Doug Mitarotonda
Yes, sorry for not being clear, for aes. I had not seen aes_string before so thanks for pointing me to that. Unfortunately, this does not look to solve my ultimate problem, which is using strings with spaces in them. I get an error where it is trying to parse the text, e.g., Error in parse(text = x) : <text>:1:9: unexpected symbol

Going back to my earlier thread, it seems like I am going the wrong direction in my desire to use strings stored as variables, particularly some with spaces in names, when it comes to plyr and ggplot2. I was able to get around my issues with ddply by creating the .. function that puts backticks in the string, but that is not looking to work here for aes. Should I be aborting this approach and just create mappings between "valid" data.frame names and what I want to print as column names/labels?

To unsubscribe from this group, send email to manipulatr+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.
Reply all
Reply to author
Forward
0 new messages