1

In base package, I can substitute "." to "_" with the following code,

names(iris)<-sub("\\.", "_", names(iris))

or

names(iris)<-sub(".", "_", fixed = T,names(iris))

How can I achieve this in dplyr with verb select?

I saw one answer

iris %>% rename_(.dots=setNames(names(.), tolower(gsub("\\.", "_", names(.)))))

from this link:R dplyr: rename variables using string functions. It seems a bit complicated, any simple solutions?

Community
  • 1
  • 1
johnsonzhj
  • 517
  • 1
  • 5
  • 23
  • 1
    Don't know how you can use `select` or why you want to use `select` in this case, but I'd suggest `iris %>% setNames(., sub("\\.", "_", names(iris)))`. `setNames` is not a `dplyr` function, but helps if you want to find something to include in a bigger piped `(%>%)` process. – AntoniosK Nov 06 '15 at 09:37
  • 1
    Or a slight modification of AntoniosK's suggestion: `iris %>% setNames(sub("[.]", "_", names(.)))` – talat Nov 06 '15 at 11:28
  • Definitely `names(.)` to make it more general! How did I miss that? :-) – AntoniosK Nov 06 '15 at 13:18
  • 1
    @AntoniosK Presumably, the OP knows that one already, since I posted it as an answer in the question they linked ;p I think the answer here is: no, dplyr believes that you are wrong to want to do this and is punishing you with this convoluted syntax. – Frank Nov 06 '15 at 13:49
  • 1
    @Frank yes, now I bet he knows that one! :-D ....or `dplyr` believes that you don't need another command to do something you can do with `setNames`. If those packages could speak, our life would be so much easier.... – AntoniosK Nov 06 '15 at 13:54

0 Answers0