rename with dots in name

842 views
Skip to first unread message

Brandon Hurr

unread,
Oct 17, 2014, 12:14:56 PM10/17/14
to manipulatr
Trying to use rename() to rename columns that I've merged using inner_join() and found I can't rename columns which have "."'s in the name. I import data from all over the place and check.names/make.names() puts in "."'s all over the place. They don't mean much to me, but they cause issues in dplyr. 

Here is an example: 

library(dplyr)

asdf<-data.frame(a.b = runif(10), c.d=runif(10))

asdf

rename(asdf, c("a.b" = "ab"))
#Error in rename_vars_(names(.data), dots) : 
#  All arguments to rename must be named.FALSE

What's the best way around this? 

Thanks, 

Brandon

Orlando Mézquita

unread,
Oct 17, 2014, 6:37:57 PM10/17/14
to manip...@googlegroups.com
Hi Brandon,
 
Check if this works:
 
rename(asdf, ab = a.b)

Bob

unread,
Oct 18, 2014, 9:56:46 AM10/18/14
to manip...@googlegroups.com
That should fix it, but it points out an inconsistency in dplyr. When doing a join and specifying "by" variables to match on that have different names the rename-like syntax follows a named vector approach while the very similar concept in the rename function does not.  -Bob

Hadley Wickham

unread,
Oct 20, 2014, 9:05:40 AM10/20/14
to Bob, manipulatr
Yes, I think the join functions might need to change in future

inner_join(x, y, c("a" = "b"))

might become

inner_join(x, y, a == b)
# or maybe
inner_join(x, y, x$a == y$b)

# That also paves the way for non-equijoins
inner_join(x, y, (a - b) < 1)

It will be a challenge to switch to this new system without breaking
existing code.

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 http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

Brandon Hurr

unread,
Oct 20, 2014, 10:27:52 AM10/20/14
to Hadley Wickham, Bob, manipulatr
Thanks to Orlando for pointing out how to do it right. Looking at the examples in the documentation I can see that this is exactly how it is done, but I guess my eyes glazed over. I assumed it was the same as inner_join and was met with the error. 

Consistency across functions would be sweet and has potential for less confusion, but it sounds like that is some time away from now. 

Thanks, 

Brandon 



Reply all
Reply to author
Forward
0 new messages