library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
Run Code Online (Sandbox Code Playgroud)
这是我们在这里使用的数据框。
# A tibble: 30 x 2
cal.date random_num
<date> <dbl>
1 2009-10-14 4.87
2 2009-10-15 8.92
3 2009-10-16 3.82
4 2009-10-17 16.0
5 2009-10-18 9.65
6 2009-10-19 3.90
7 2009-10-20 10.4
8 2009-10-21 11.7
9 2009-10-22 10.9
10 2009-10-23 6.47
# ... with 20 more rows
Run Code Online (Sandbox Code Playgroud)
我试图嵌套(sp? lexical scope)两个函数,我称之为child_function和parent_function。
child_function <- function(df, variable, hor.line = 6) {
variable …Run Code Online (Sandbox Code Playgroud) 我经常遇到一个问题,即在连接后合并重复列的非 NA 值并删除重复项。它类似于这个问题或这个问题中所描述的内容。我想围绕coalesce(并可能包括left_join)创建一个小函数,以便在遇到它时在一行中处理它(函数本身当然可以根据需要长度)。
这样做时,我遇到了缺乏此处描述的quo_names等效内容的情况。quos
对于 reprex,采用带有识别信息的数据帧,与包含正确值但经常拼写错误的 ID 的其他数据帧连接。
library(dplyr)
library(rlang)
iris_identifiers <- iris %>%
select(contains("Petal"), Species)
iris_alt_name1 <- iris %>%
mutate(Species = recode(Species, "setosa" = "stosa"))
iris_alt_name2 <- iris %>%
mutate(Species = recode(Species, "versicolor" = "verscolor"))
Run Code Online (Sandbox Code Playgroud)
这个更简单的函数可以工作:
replace_xy <- function(df, var) {
x_var <- paste0(var, ".x")
y_var <- paste0(var, ".y")
df %>%
mutate(!! quo_name(var) := coalesce(!! sym(x_var), !! sym(y_var))) %>%
select(-(!! sym(x_var)), -(!! sym(y_var)))
}
iris_full <- …Run Code Online (Sandbox Code Playgroud) 我正在做一些有点简单的事情,但 tidyeval 总是让我困惑。在本例中,我有一个可以绘制某些内容的函数,并且之后我还想使用我正在绘制的列的名称来保存它,如下所示:
bar_plot= function(table, col_plot){
ggplot(table, aes(x=region,
y= {{col_plot}})) +
geom_bar(stat = "identity", fill="steelblue") +
ggsave(glue('results/{col_plot}.png'))
}
Run Code Online (Sandbox Code Playgroud)
该图没有问题,但我无法保存它(找不到该对象,因为它没有将其作为字符串读取)。尝试使用、、、quo但没有任何效果。在函数内将变量名转换为字符串的方法是什么?enquosym
对于可重复性来说,这已经足够了:
df = data.frame(region = c(1, 2), mean_age = c(20, 30))
Run Code Online (Sandbox Code Playgroud)
谢谢 !
语境
我正在这个网站上学习整洁评估,我看到一个例子:
x <- sym("height")
expr(transmute(starwars, bmi = mass / (!! x)^2))
#> transmute(starwars, bmi = mass/(height)^2)
transmute(starwars, bmi = mass / (!! x)^2)
#> # A tibble: 87 x 1
#> bmi
#> <dbl>
#> 1 26.0
#> 2 26.9
#> 3 34.7
#> 4 33.3
#> # ... with 83 more rows
Run Code Online (Sandbox Code Playgroud)
sym然后,我在自己的代码中使用and模仿了上面的例子!!,但是报错了。
library(survival)
library(rms)
data(cancer)
x = sym('meal.cal')
expr(cph(Surv(time, status) ~ rcs(!! x), data = lung))
# cph(Surv(time, status) ~ rcs(meal.cal), data …Run Code Online (Sandbox Code Playgroud) 考虑这个简单的例子
library(dplyr)
dataframe <- data_frame(id = c(1,2,3,4),
group = c('a','b','c','c'),
value = c(200,400,120,300))
> dataframe
# A tibble: 4 x 3
id group value
<dbl> <chr> <dbl>
1 1 a 200
2 2 b 400
3 3 c 120
4 4 c 300
Run Code Online (Sandbox Code Playgroud)
这个tidyeval函数用于dplyr根据某些输入列聚合我的数据框。
func_tidy <- function(data, mygroup){
quo_var <- enquo(mygroup)
df_agg <- data %>%
group_by(!!quo_var) %>%
summarize(mean = mean(value, na.rm = TRUE),
count = n()) %>%
ungroup()
df_agg
}
Run Code Online (Sandbox Code Playgroud)
现在,这有效
> func_tidy(dataframe, group) …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个tidyeval函数,该函数接受一个数字列,用值替换某个值以上limit的值limit,将该列转换为一个因子,然后将因子级别替换为limit一个名为"limit +"的级别.
例如,我试图用sepal.width替换3以上的任何值,然后将该因子级别重命名为3+.
作为一个例子,这是我试图使它与虹膜数据集一起工作的方式.但是,fct_recode()函数没有正确地重命名因子级别.
plot_hist <- function(x, col, limit) {
col_enq <- enquo(col)
x %>%
mutate(var = factor(ifelse(!!col_enq > limit, limit,!!col_enq)),
var = fct_recode(var, assign(paste(limit,"+", sep = ""), paste(limit))))
}
plot_hist(iris, Sepal.Width, 3)
Run Code Online (Sandbox Code Playgroud) 我的数据框如下所示:
df <- tibble(x = c(1, 2, NA),
y = c(1, NA, 3),
z = c(NA, 2, 3))
Run Code Online (Sandbox Code Playgroud)
我想使用tidyr :: replace_na()将NA替换为0。正如该函数的文档所阐明的那样,一旦知道要在哪个列上执行操作,就可以很容易地做到这一点。
df <- df %>% replace_na(list(x = 0, y = 0, z = 0))
Run Code Online (Sandbox Code Playgroud)
但是,如果列数不确定,该怎么办?(之所以说“不确定”,是因为我试图创建一个使用dplyr工具即时执行此操作的函数。)如果我没记错的话,等效于我要使用上述工具实现的基数R是:
df[, 1:ncol(df)][is.na(df[, 1:ncol(df)])] <- 0
Run Code Online (Sandbox Code Playgroud)
但是我总是很难理解这个代码。在此先感谢您的帮助。
讨论ggplot2中的整洁评估的文章给人的印象是aes()现在支持准化。但是,我无法使其与unquote-splice运算符一起使用!!!。
library( ggplot2 )
## Predefine the mapping of symbols to aesthetics
v <- rlang::exprs( x=wt, y=mpg )
## Symbol-by-symbol unquoting works without problems
ggplot( mtcars, aes(!!v$x, !!v$y) ) + geom_point()
## But unquote splicing doesn't...
ggplot( mtcars, aes(!!!v) ) + geom_point()
# Error: Can't use `!!!` at top level
# Call `rlang::last_error()` to see a backtrace
Run Code Online (Sandbox Code Playgroud)
(也许不足为奇)如果将美学贴图移动到几何体,则会发生相同的事情:
ggplot( mtcars ) + geom_point( aes(!!v$x, !!v$y) ) # works
ggplot( mtcars ) + geom_point( aes(!!!v) ) …Run Code Online (Sandbox Code Playgroud) 我想让以下函数使用furrr包而不是purrr包并行运行。
library(furrr)
library(tidyverse)
input <- list(element1 = tibble::tibble(a = c(1, 2), b = c(2, 2)),
element2 = tibble::tibble(a = c(1, 2), b = c(4, 4))
)
multiplier <- function(data, var1, var2){
purrr::map_df(.x = data,
.f = ~ .x %>%
dplyr::mutate(product = {{var1}} * {{var2}})
)
}
multiplier(input, a, b)
Run Code Online (Sandbox Code Playgroud)
但是,当我将其转换为furrr等效项时,出现错误。
multiplier_parallel <- function(data, var1, var2){
furrr::future_map_dfr(.x = data,
.f = ~ .x %>%
dplyr::mutate(product = {{var1}} * {{var2}})
)
}
future::plan(multiprocess)
multiplier_parallel(input, a, b) …Run Code Online (Sandbox Code Playgroud) 当存在多个分组变量时,group_by 的数据屏蔽不起作用。
下面粘贴代码
grpByCols <- "model"
mpg%>%
group_by(.data[[grpByCols]])
grpByCols <- c("model", "manufacturer")
mpg%>%
group_by(.data[[grpByCols]])
Run Code Online (Sandbox Code Playgroud)
第一个 group_by 有效,第二个失败。
粘贴下面的运行输出
> grpByCols <- "model"
>
> mpg%>%
+ group_by(.data[[grpByCols]])
# A tibble: 234 x 11
# Groups: model [38]
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 …Run Code Online (Sandbox Code Playgroud)