r语言 - dplyr非标准评估 - 需要帮助



我正在dplyr中通过非标准评估(NSE(迈出我的第一步。 考虑以下代码片段:它需要一个tibble,根据列中的值对其进行排序,并将 n-k 较低的值替换为"其他"。

例如,请参阅:

library(dplyr)
df <- cars%>%as_tibble
k <- 3
df2 <- df %>%
arrange(desc(dist))  %>% 
mutate(dist2 = factor(c(dist[1:k],
rep("Other", n() - k)),
levels = c(dist[1:k], "Other")))

我想要的是一个这样的函数:

df2bis<-df %>% sort_keep(old_column, new_column, levels_to_keep)

产生相同的结果,其中old_column列"dist"(我用来对数据集进行排序的列(,new_column列(我生成的列(是"dist2",levels_to_keep是"k"(我显式保留的值的数量(。 我迷失在enquo,quo_name等...

任何建议不胜感激。

你可以做:

library(dplyr)
sort_keep=function(df,old_column, new_column, levels_to_keep){
old_column = enquo(old_column)
new_column = as.character(substitute(new_column))
df %>%
arrange(desc(!!old_column))  %>% 
mutate(use = !!old_column,
!!new_column := factor(c(use[1:levels_to_keep],
rep("Other", n() - levels_to_keep)),
levels = c(use[1:levels_to_keep], "Other")),
use=NULL)
}

df%>%sort_keep(dist,dist2,3)

像这样的东西?

old_column = "dist"
new_column = "dist2"
levels_to_keep = 3
command = "df2bis<-df %>% sort_keep(old_column, new_column, levels_to_keep)"
command = gsub('old_column', old_column, command)
command = gsub('new_column', new_column, command)
command = gsub('levels_to_keep', levels_to_keep, command)
eval(parse(text=command))

最新更新