RDJ RDJ - 1 year ago 129
R Question

Dplyr or Magrittr - tolower?

Is it possible to set all column names to upper or lower within a dplyr or magrittr chain?

In the example below I load the data and then, using a magrittr pipe, chain it through to my dplyr mutations. In the 4th line I use the

function , but this is for a different purpose: to create a new variable with lowercase observations.

mydata <- read.csv('myfile.csv') %>%
mutate(Year = mdy_hms(DATE),
Reference = (REFNUM),
Event = tolower(EVENT)

I'm obviously looking for something like
colnames = tolower
but know this doesn't work/exist.

I note the dplyr
function but this isn't really helpful.

In magrittr the colname options are:

instead of base R's

instead of base R's

I've tried numerous permutations with these but no dice.

Obviously this is very simple in base r.

names(mydata) <- tolower(names(mydata))

However it seems incongruous with the / philosophies that you'd have to do that as a clunky one liner, before moving on to an elegant chain of dplyr/magrittr code.

Answer Source

Using magrittr's "compound assignment pipe-operator" %<>% might be, if I understand your question correctly, an even more succinct option.

names(iris) %<>% tolower

?`%<>%` # for more
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download