Dplyr或Magrittr? [英] Dplyr or Magrittr - tolower?

查看:130
本文介绍了Dplyr或Magrittr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在dplyr或magrittr链中将所有列名称设置为高或低?

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

在下面的示例中,我加载数据,然后使用magrittr管,链通过我的dplyr突变。在第四行中,我使用 tolower 函数,但这是一个不同的目的:创建一个带有小写观察值的新变量。

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 tolower 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)

我显然正在寻找类似 colnames = tolower 的东西,但知道这不行/存在。

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

我注意到dplyr 重命名函数,但这并不真正有用。

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

在magrittr中,colname选项是:

In magrittr the colname options are:

set_colnames 而不是base R的 colnames< -

set_names 而不是base R的名称< -

set_colnames instead of base R's colnames<-
set_names instead of base R's names<-

我已经尝试了许多排列,但没有骰子。

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

显然,这在基础r中非常简单。

Obviously this is very simple in base r.

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

然而,与 dplyr / magrittr 的哲学,你必须做一个笨重的一个班轮,然后再转到一个优雅的dplyr / magrittr代码链。

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

推荐答案

使用 magrittr 的复合分配管道操作符 code>%<>%可能是,如果我明白您的问题正确,一个更简洁的选择。

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

library("magrittr")
names(iris) %<>% tolower

?`%<>%` # for more

这篇关于Dplyr或Magrittr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆