【发布时间】:2021-09-06 01:04:13
【问题描述】:
所以我有这个清单:
list(`0` = structure(list(fn = 0L, fp = 34L, tn = 0L, tp = 34L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.1` = structure(list(
fn = 1L, fp = 26L, tn = 8L, tp = 33L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.2` = structure(list(
fn = 3L, fp = 22L, tn = 12L, tp = 31L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.3` = structure(list(
fn = 5L, fp = 7L, tn = 27L, tp = 29L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.4` = structure(list(
fn = 5L, fp = 3L, tn = 31L, tp = 29L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.5` = structure(list(
fn = 7L, fp = 1L, tn = 33L, tp = 27L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.6` = structure(list(
fn = 8L, fp = 0L, tn = 34L, tp = 26L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.7` = structure(list(
fn = 8L, fp = 0L, tn = 34L, tp = 26L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.8` = structure(list(
fn = 8L, fp = 0L, tn = 34L, tp = 26L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `0.9` = structure(list(
fn = 30L, fp = 0L, tn = 34L, tp = 4L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `1` = structure(list(
fn = 34L, fp = 0L, tn = 34L, tp = 0L), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")))
当我为 10 个不同的分位数应用分位数回归模型时,它基本上是一个长度为 10 的列表。每个元素都是一个包含真/假正/负计数的数据框。现在我想编写一个函数,我可以在其中“动态”计算可以使用这些计数计算的各种指标。例如,第一个元素如下所示:
> cms[[1]]
# A tibble: 1 x 4
fn fp tn tp
<int> <int> <int> <int>
1 0 34 0 34
因为它是一个列表,所以我真的很想用purrr 的map 或lapply 或类似的东西做点什么。然后我想:总有一天我想要真正的阳性率,有一天我可能想要特异性。因此,我想我会编写一个函数,它可以将一些列作为输入并执行“经典”dplyr::mutate。但是我再一次被我关于整洁评估的知识所困扰。所以我做了这样的事情(请不要评判):
fun = function(...){
f = rlang::enexpr(...)
return(f)
}
fpr = fun(tp / tp + fn)
# does not work
map(cms, ~mutate(.x, fpr=fpr))
# this (non-tidy-eval) works
map(cms, ~mutate(.x, fpr=tp / tp + fn))
我真的很想动态地传入列并使用 tidy-evaluation 计算结果。因此,我将不胜感激任何帮助或指针:)
【问题讨论】:
-
您的数据保留数据框列表是否有原因?整洁的动词对于嵌套的数据结构总是很尴尬;处理此类数据的更惯用方法是将数据帧绑定在一起(添加
quantile列以识别行的来源)并使用正常的变异函数。即使您需要列表中的数据,这样做可能更容易,然后使用split()或group_split()恢复原始结构。 -
非常感谢!我会尝试采用这种方法:)
标签: r dplyr purrr rlang tidyeval