r - 在 Dplyr 中评估多行
问题描述
我有显示变量的数据集,我想要执行的计算(总和,不同值的数量)和计算后的新变量名称。
library(dplyr)
RefDf <- read.table(text = "Variables Calculation NewVariable
Sepal.Length sum Sepal.Length2
Petal.Length n_distinct Petal.LengthNew
", header = T)
手动方法- 通过对 Species 变量分组进行总结。
iris %>% group_by_at("Species") %>%
summarise(Sepal.Length2 = sum(Sepal.Length,na.rm = T),
Petal.LengthNew = n_distinct(Petal.Length, na.rm = T)
)
通过自动化eval(parse( ))
x <- RefDf %>% mutate(Check = paste0(NewVariable, " = ", Calculation, "(", Variables, ", na.rm = T", ")")) %>% pull(Check)
iris %>% group_by_at("Species") %>% summarise(eval(parse(text = x)))
截至目前,它正在回归 -
Species `eval(parse(text = x))`
<fct> <int>
1 setosa 9
2 versicolor 19
3 virginica 20
它应该返回 -
Species Sepal.Length2 Petal.LengthNew
<fct> <dbl> <int>
1 setosa 250. 9
2 versicolor 297. 19
3 virginica 329. 20
解决方案
您可以使用parse_exprs
:
library(tidyverse)
library(rlang)
RefDf <- read.table(text = "Variables Calculation NewVariable
Sepal.Length sum Sepal.Length2
Petal.Length n_distinct Petal.LengthNew
", header = T)
#
expr_txt <- set_names(str_c(RefDf$Calculation, "(", RefDf$Variables, ")"),
RefDf$NewVariable)
iris %>%
group_by_at("Species") %>%
summarise(!!!parse_exprs(expr_txt), .groups = "drop")
## A tibble: 3 x 3
#Species Sepal.Length2 Petal.LengthNew
#<fct> <dbl> <int>
#1 setosa 250. 9
#2 versicolor 297. 19
#3 virginica 329. 20
推荐阅读
- typescript - 我可以从 ionic 中的 post api 获取数据吗?
- flutter - 如何干净/正确地实现嵌套的 TabBarView?
- angular - js-xls - Angular 上的工作表到 json 只返回第一行
- mysql - MySQL8 从物理文件恢复 - Innodb
- r - R (RShiny) 等效于其他类型绘图的 layer_data 函数
- wikimedia - 使用 get_db_data 重现 select distinct
- android - java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=65637, result=-1, data=Intent
- graphql - 带变量的 Apollo 查询
- json - 无法读取角度 5 中的 json 文件?
- azure - 日志分析以收集和分析 IIS 日志