我正在尝试使用quores来传递自定义函数中的变量名以进行数据处理和在公式中使用,但是在公式中使用quosures是不正确的。有没有更好的方法可以在公式中取消引号的引用?
library(dplyr)
library(broom)
library(purrr)
library(tidyr)
foo <- function(mydata, dv, iv, group_var) {
dv = enquo(dv)
iv = enquo(iv)
group_var = enquo(group_var)
mydata <- mydata %>%
group_by(!!group_var) %>%
nest()
mydata %>%
mutate(model = map(data,
~summary(lm(formula(substitute(dv ~ iv)), data = .))
)) %>%
unnest(model %>% map(tidy))
}
foo(mydata=mtcars, dv=mpg, iv=wt, group_var=cyl)
Run Code Online (Sandbox Code Playgroud)
我的代码产生“ mutate_impl(.data,点)中的错误:评估错误:对象不是矩阵。”
这是我要制作成函数的代码的工作版本:
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(model = map(data, ~summary(lm(mpg ~ wt, data = .)))) %>%
unnest(model %>% map(tidy))
Run Code Online (Sandbox Code Playgroud) 这纯粹是一个概念性的问题,但是如何在使用.$或.[]的计算中引用我的向量c(2,3)的元素?
library(tidyverse)
c(2, 3) %>%
.[1] * .[2]
Run Code Online (Sandbox Code Playgroud)
此代码工作正常但需要创建临时对象(v):
v <- c(2,3)
v[1] * v[2]
Run Code Online (Sandbox Code Playgroud)
我想知道如何在tidyverse中执行计算而不创建临时对象v.
我想为数据集中的所有变量生成一系列直方图,但是显然我没有正确准备要在map函数中使用的数据。
library(tidyverse)
mtcars %>%
select(wt, disp, hp) %>%
map(., function(x)
ggplot(aes(x = x)) + geom_histogram()
)
Run Code Online (Sandbox Code Playgroud)
我可以使用for循环(h / t,但尝试在tidyverse中做同样的事情来完成此任务。
foo <- function(df) {
nm <- names(df)
for (i in seq_along(nm)) {
print(
ggplot(df, aes_string(x = nm[i])) +
geom_histogram())
}
}
mtcars %>%
select(wt, disp, hp) %>%
foo(.)
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏。
在基本RI中,可以使用以下代码删除/替换整个数据帧中的值(例如,所有NA或大于0.99的值)。
df[df > 0.99] <- NA
df[is.na(df)] <- 0L
Run Code Online (Sandbox Code Playgroud)
有没有办法在tidyverse中使用dplyr执行等效操作?
在这个例子中,我想将count()函数应用于数据集中的每个字符变量.
library(dplyr)
library(purrr)
nycflights13::flights %>%
select_if(is.character) %>%
map(., count)
Run Code Online (Sandbox Code Playgroud)
但是我收到错误消息:
Error in UseMethod("groups") : no applicable method for
'groups' applied to an object of class "character"
Run Code Online (Sandbox Code Playgroud)
我不确定如何解释错误消息或更新我的代码.类似的代码适用于数字变量,但因子变量会对字符变量产生类似的错误消息
nycflights13::flights %>%
select_if(is.numeric) %>%
map(., mean, na.rm = TRUE)
nycflights13::flights %>%
select_if(is.character) %>%
mutate_all(as.factor) %>%
map(., count)
Run Code Online (Sandbox Code Playgroud) 在下面的图中,我想将标签“V-Engine”移到图边距中。调整nudge_x参数是移动“S-Engine”标签而不是“V-Engine”标签。
library(ggplot2)
library(ggrepel)
library(dplyr)
ds <-
mtcars %>%
mutate(vs = factor(vs, labels = c("V-Engine", "S-Engine"))) %>%
# Create labels for the rightmost data points
group_by(vs) %>%
mutate(
label =
case_when(
wt == max(wt) ~ as.character(vs),
TRUE ~ NA_character_
)
) %>%
ungroup()
ds %>%
ggplot(aes(x = wt, y = mpg, color = vs)) +
geom_smooth(se=FALSE) +
geom_label_repel(aes(label = label), nudge_x = 1, na.rm = TRUE) +
guides(color = FALSE) +
theme_minimal() +
theme(plot.margin = unit(c(1,3,1,1), "cm"))
Run Code Online (Sandbox Code Playgroud)
我不清楚何时可以在 pmap() 和 pwalk() 的.l参数中明确配对参数。有时,这些 purrr 函数似乎仅在提供给它们的数据帧具有直接映射到名为 in 的函数的预期参数的名称时才起作用.f。其他时候可以将完整的数据帧提供给 pmap() 并且可以显式地对变量进行配对映射。
library(dplyr)
library(purrr)
library(tibble)
set.seed(57)
ds_mt <-
mtcars %>%
rownames_to_column("model") %>%
mutate(am = factor(am, labels = c("auto", "manual"))) %>%
select(model, mpg, wt, cyl, am) %>%
sample_n(3)
foo <- function(model, am, mpg){
print(
paste("The", model, "has a", am, "transmission and gets", mpg, "mpgs.")
)
}
Run Code Online (Sandbox Code Playgroud)
为什么这些代码块有效?
ds_mt %>%
select(model, am, mpg) %>%
pwalk(
.l = .,
.f = foo
)
# example with explicit pair mapping
ds_mt …Run Code Online (Sandbox Code Playgroud) 我可以使用 调用scale_y_continuous() 中的匿名函数function(y) comma(y),但无法使用 ~ 约定调用匿名函数。这种情况下可以用~吗?
library(scales)
library(ggplot2)
mtcars$model <- rownames(mtcars)
# Success
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = function(y) comma(y))
# Fail
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = ~comma(y))
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 CTE 为 SQL (postgresql) 中的单元测试生成数据表。
WITH temp1 AS (
SELECT
('A', 'A', 'B', 'B') AS grp,
(1, 2, NULL, 1) AS outcome
)
SELECT *
FROM temp1
Run Code Online (Sandbox Code Playgroud)
上面的查询生成单行而不是对我的单元测试有用的 4 行表。如何生成以下形式的 4 行表:
grp.....outcome
A.......1
A.......2
B.......NULL
B.......1
Run Code Online (Sandbox Code Playgroud) 有几个代码片段对我的工作流程非常宝贵,并且可以与我的自定义 R 包中的函数很好地配合。我可以将这些代码片段包含在我的 R 包中,以便在用户安装我的包时将它们添加到用户的代码片段中(当然需要权限)吗?
创建 sql 块的 Rmd 片段示例:
snippet sql
```{sql, connection = conn, output.var = "${1:df}"}
${2}
```
Run Code Online (Sandbox Code Playgroud)