Dplyr 非标准评估——需要帮助
Dplyr Non Standard Evaluation -- Help Needed
我正在 dplyr
中使用非标准评估 (NSE) 迈出我的第一步。
考虑以下代码片段:它采用 tibble
,根据列内的值对其进行排序,并将 n-k 个较低的值替换为 "Other"。
例如见:
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 等等...
欢迎任何建议。
是这样的吗?
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))
你可以这样做:
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)
我正在 dplyr
中使用非标准评估 (NSE) 迈出我的第一步。
考虑以下代码片段:它采用 tibble
,根据列内的值对其进行排序,并将 n-k 个较低的值替换为 "Other"。
例如见:
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 等等...
欢迎任何建议。
是这样的吗?
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))
你可以这样做:
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)