在dplyr 0.7+函数中重命名 [英] Rename in dplyr 0.7+ function

查看:156
本文介绍了在dplyr 0.7+函数中重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在使用dplyr的函数中重命名列。我已经在非标准评估和使用enquo上找到有用的帖子(例如 http:// dplyr .tidyverse.org / articles / programming.html 更改自定义dplyr函数中结果变量的名称)。最终目标是使用该函数来汇总每个组,然后将列重命名为比原始变量名称更有意义的内容。



以下是一个简单的例子,除了注释掉的行外,其他的行都可以。

  library(tidyverse)

myfunc< - function(df,groupvar,colvar,grouplab,collab){
groupvar< - enquo(groupvar)
colvar< - enquo(colvar)
grouplab< - enquo(grouplab)#quo_name改为?
合作< - enquo(collab)#quo_name代替?

t < - df%>%
select(!! groupvar,!! colvar)%>%
group_by(!! groupvar,!! colvar)%> ;%
汇总(
频率=长度(!! colvar),
百分比=长度(!! colvar)/ nrow(df)
)%>%$ b $ (
Frequencies = scales ::逗号(Frequencies),
百分比= scales :: percent(百分比)
)#%>%
#重命名(
#(!! grouplab)= !! groupvar,#添加一个更具描述性的分组变量标签
#(!! collab)= !! colvar)#添加一个更具描述性的列var标签

#错误:意想不到的'='in:rename((!!grouplab)=

...#创建xtable的代码,将其打印到LaTeX
}

myfunc< - (df = mtcars,groupvar = gear,colvar = cyl,grouplab =Vehicle Gears,collab =Vehicle Cylinders)
#我故意让grouplab和collab有空间这种情况会改变任何东西。

除了函数外,相关部分只会读取: mtcars< - mtcars%>%rename(Vehicle Cylinders= cyl,Vehicle Gears= gear)

我必须越来越意识到重命名是如何在一个函数中运行的,而不是在其外部。有什么建议?我接受建议。

更新

这是一种方法。我宁愿不把变量名称放在字符串中,但不知道在这里绕过它。

  library(tidyverse)
library(xtable)
data(mtcars)

t_groupedfreqprop < - 函数(df,groupvar,colvar,grouplab,collab,plotname){
groupvar2 < - rlang :: sym(groupvar)
colvar2 < - rlang: :sym(colvar)
grouplab< - rlang :: sym(grouplab)
collab< - rlang :: sym(collab)

t< - df%> %
select(!! groupvar2,!! colvar2)%>%
group_by(!! groupvar2,!! colvar2)%>%
汇总(
频率=长度(!! colvar2),
Percentages =长度(!! colvar2)/ nrow(df)
)%>%
mutate(
Frequencies = scales :: comma(Frequencies ),
Percentages = scales :: percent(Percentages)
)%>%
重命名(
!! grouplab:= !! rlang :: sym(groupvar2),
!! collab:= !! rlang :: sym(colvar2)


#被删除的函数的其余部分
}

t_groupedfreqprop( df = mtcars,groupvar =gear,colvar =cyl,grouplab =Veh icle Gears,collab =Vehicle Cylinders)


解决方案

使用:= ,如果您希望在表达式的LHS上以编程方式指定名称。

 <$ c 
重新命名(mtcars,!! xnew:= !! rlang :: sym(x))
xnew < - mpg2 code>


I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting variables in custom dplyr function). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names.

Here is a simple example, which works except for the commented-out lines.

library(tidyverse)

myfunc <- function(df, groupvar, colvar, grouplab, collab) {
  groupvar <- enquo(groupvar)
  colvar <- enquo(colvar)
  grouplab <- enquo(grouplab)  # quo_name instead?
  collab <- enquo(collab)  # quo_name instead?

t <- df %>%
  select(!!groupvar, !!colvar) %>%
  group_by(!!groupvar, !!colvar) %>%
  summarise(
    Frequencies = length(!!colvar),
    Percentages = length(!!colvar) / nrow(df)
    ) %>%
  mutate(
    Frequencies = scales::comma(Frequencies),
    Percentages = scales::percent(Percentages)
    ) #%>%
# rename(
#    (!!grouplab) = !!groupvar,  # add a more descriptive grouping var label
#    (!!collab) = !!colvar)  # add a more descriptive column var label

# Error: unexpected '=' in: rename((!!grouplab) = "

...  # code to create an xtable, print it into LaTeX
}

myfunc <- (df = mtcars, groupvar = gear, colvar = cyl, grouplab = "Vehicle Gears", collab = "Vehicle Cylinders")
# I purposely made grouplab and collab have a space in case that changes anything. 

Outside of functions, the relevant part would read simply: mtcars <- mtcars %>% rename("Vehicle Cylinders" = cyl, "Vehicle Gears" = gear)

I must be getting tripped up in how renaming operates in a function vs. outside of it. Any advice? I'm open to suggestions.

UPDATE
Here is one way to do it. I'd rather not put the variable names in strings, but don't know a way around it here. Open to suggestions.

library(tidyverse)
library(xtable)
data(mtcars)

t_groupedfreqprop <- function(df, groupvar, colvar, grouplab, collab, plotname) {
groupvar2 <- rlang::sym(groupvar)
colvar2 <- rlang::sym(colvar)
grouplab <- rlang::sym(grouplab)
collab <- rlang::sym(collab)

t <- df %>%
  select(!!groupvar2, !!colvar2) %>%
  group_by(!!groupvar2, !!colvar2) %>%
  summarise(
    Frequencies = length(!!colvar2),
    Percentages = length(!!colvar2) / nrow(df)
  ) %>%
  mutate(
    Frequencies = scales::comma(Frequencies),
    Percentages = scales::percent(Percentages)
  ) %>%
  rename(
    !!grouplab := !!rlang::sym(groupvar2),
    !!collab := !!rlang::sym(colvar2)
  )

# remainder of function removed
}

t_groupedfreqprop(df = mtcars, groupvar = "gear", colvar = "cyl", grouplab = "Vehicle Gears", collab = "Vehicle Cylinders")

解决方案

Use := if you want programmatically-assigned names on the LHS of expressions.

x <- "mpg"
xnew <- "mpg2"
rename(mtcars, !!xnew := !!rlang::sym(x))

这篇关于在dplyr 0.7+函数中重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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