dplyr 编程:取消引用拼接会导致complete() 和nesting() 出现超出范围的错误

2024-04-16

所以我开始涉足 dplyr 编程的奇妙世界。我正在尝试编写一个接受 data.frame、目标列和任意数量的分组列(对所有列使用裸名称)的函数。然后,该函数将根据目标列对数据进行分箱,并计算每个分箱中的条目数。我想为原始 data.frame() 中存在的分组变量的每个组合保留单独的 bin 大小,因此我使用complete() 和nesting() 函数来执行此操作。这是我正在尝试执行的操作以及遇到的错误的示例:

library(dplyr)
library(tidyr)

#Prepare test data
set.seed(42)
test_data =
    data.frame(Gene_ID = rep(paste0("Gene.", 1:10), times=4),
               Comparison = rep(c("WT_vs_Mut1", "WT_vs_Mut2"), each=10, times=2),
               Test_method = rep(c("T-test", "MannWhitney"), each=20),
               P_value = runif(40))

#Perform operation manually
test_data %>% 
    #Start by binning the data according to q-value
    mutate(Probability.bin = cut(P_value,
                                 breaks = c(-Inf, seq(0.1, 1, by=0.1), Inf),
                                 labels = c(seq(0.0, 1.0, by=0.1)),
                                 right = FALSE)) %>% 
    #Now summarize the results by bin.
    count(Comparison, Test_method, Probability.bin) %>% 
    #Fill in any missing bins with 0 counts
    complete(nesting(Comparison, Test_method), Probability.bin,
             fill=list(n = 0))

#Create function that accepts bare column names
bin_by_p_value <- function(df,
                           pvalue_col, #Bare name of p-value column
                           ...) {      #Bare names of grouping columns

    #"Quote" column names so they are ready for use below
    pvalue_col_name <- enquo(pvalue_col)
    group_by_cols <- quos(...)

    #Perform the operation
    df %>% 
        #Start by binning the data according to q-value
        mutate(Probability.bin = cut(UQ(pvalue_col_name),
                                     breaks = c(-Inf, seq(0.1, 1, by=0.1), Inf),
                                     labels = c(seq(0.0, 1.0, by=0.1)),
                                     right = FALSE)) %>% 
        #Now summarize the results by bin.
        count(UQS(group_by_cols), Probability.bin) %>% 
        #Fill in any missing bins with 0 counts
        complete(nesting(UQS(group_by_cols)), Probability.bin,
                 # complete(nesting(UQS(group_by_cols)), Probability.bin,
                 fill=list(n = 0))
}

#Use function to perform operation
test_data %>% 
    bin_by_p_value(P_value, Comparison, Test_method)

当我手动执行操作时,一切正常。当我使用该函数时,它失败并出现以下错误:

overscope_eval_next(overscope, expr) 中的错误: 未找到对象“比较”

我已将问题范围缩小到函数中的以下代码:

complete(nesting(UQS(group_by_cols)), Probability.bin...

如果我删除对 Nesting() 的调用,则代码执行时不会出现错误。但是,我想保留仅使用原始数据中存在的分组变量的组合的功能,然后获取所有可能的容器组合,以便我可以填充所有缺失的容器。根据错误名称和失败的地方,我的猜测是这是一个范围/环境问题,我真的应该为嵌套()中的分组变量使用不同的环境,因为它包含在对complete()的调用中。然而,我对 dplyr 编程还很陌生,我不知道该怎么做。

我尝试通过将分组列合并为单个列,然后使用该联合列作为complete() 的输入来解决此问题。这让我可以按照自己想要的方式执行complete() 操作,同时避免使用nesting() 函数。然而,当我想分离回原始分组列时,我遇到了麻烦,因为我不知道如何将定额列表转换为字符向量(分离()的“into”参数所需)。以下是代码片段来说明我正在谈论的内容:

        #Fill in any missing bins with 0 counts
        unite(Merged_grouping_cols, UQS(group_by_cols), sep="*") %>% 
        complete(Merged_grouping_cols, Probability.bin,
                 fill=list(n = 0)) %>%
        separate(Merged_grouping_cols, into=c("What goes here?"), sep="\\*")

以下是相关版本信息:R 版本 3.4.2 (2017-09-28)、tidyr_0.7.2、dplyr_0.7.4

我很感激任何解决方法,但我想知道我在做什么以错误的方式摩擦complete()和nesting()。


  • 使用卷曲卷曲{{}} for pvalue_col.
  • 传递点(...)直接到count.
  • Use ensyms with !!! in nesting.
bin_by_p_value <- function(df,
                           pvalue_col, #Bare name of p-value column
                           ...) {      #Bare names of grouping columns
  
  #Perform the operation
  df %>% 
    #Start by binning the data according to q-value
    mutate(Probability.bin = cut({{pvalue_col}},
                                 breaks = c(-Inf, seq(0.1, 1, by=0.1), Inf),
                                 labels = c(seq(0.0, 1.0, by=0.1)),
                                 right = FALSE)) %>% 
    #Now summarize the results by bin.
    count(..., Probability.bin) %>% 
    #Fill in any missing bins with 0 counts
    complete(nesting(!!!ensyms(...)), Probability.bin,   fill=list(n = 0))
}

test_data %>% bin_by_p_value(P_value, Comparison, Test_method)

# A tibble: 44 x 4
#   Comparison Test_method Probability.bin     n
#   <chr>      <chr>       <fct>           <dbl>
# 1 WT_vs_Mut1 MannWhitney 0                   1
# 2 WT_vs_Mut1 MannWhitney 0.1                 1
# 3 WT_vs_Mut1 MannWhitney 0.2                 0
# 4 WT_vs_Mut1 MannWhitney 0.3                 1
# 5 WT_vs_Mut1 MannWhitney 0.4                 1
# 6 WT_vs_Mut1 MannWhitney 0.5                 1
# 7 WT_vs_Mut1 MannWhitney 0.6                 0
# 8 WT_vs_Mut1 MannWhitney 0.7                 0
# 9 WT_vs_Mut1 MannWhitney 0.8                 1
#10 WT_vs_Mut1 MannWhitney 0.9                 4
# … with 34 more rows

测试手动调用的输出是否存储在res.

identical(res, test_data %>% bin_by_p_value(P_value, Comparison, Test_method))
#[1] TRUE
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dplyr 编程:取消引用拼接会导致complete() 和nesting() 出现超出范围的错误 的相关文章

  • 需要在R中跳过不同数量的行

    我正在使用以下代码来处理我的数据 但最近我意识到使用skip 27 在数据开始之前跳过存储在我的文件中的信息 不是一个好的选择 因为每个文件中要跳过的行数不同我的目标是读取存储在多个文件夹中的各种txt文件 并非所有文件都有相同的列数 列的
  • 扩展数据框以使其具有与原始行中两列的范围一样多的行[重复]

    这个问题在这里已经有答案了 我有一个数据框如下 structure list symbol c u n v i a start c 9L 6L 10L 8L 7L end c 14L 15L 12L 13L 11L Names c symb
  • 如何动态地将 sliderInput 添加到闪亮的应用程序中?

    使用闪亮 我上传一个 csv 文件 并根据列名称 我需要向 ui 添加滑块 sidebarPanel fileInput file1 Upload CSV File to Create a Model accept c text csv t
  • 如何将旋转的 NetCDF 转换回正常的纬度/经度网格?

    我有一个带有旋转坐标的 NetCDF 文件 我需要将其转换为正常的纬度 经度坐标 经度为 180到180 纬度为 90到90 library ncdf4 nc open dat nf 对于尺寸 它显示 1 5 variables exclu
  • R - 基于列名称的子集

    我的数据框有超过 120 列 变量 我想根据列名称创建子集 例如 我想创建一个子集 其中列名称包含字符串 心情 这可能吗 我一般用 SubData lt myData grep whatIWant colnames myData 我很清楚
  • rvest 函数 html_nodes 返回 {xml_nodeset (0)}

    我正在尝试抓取以下网站的数据框 http stats nba com game 0041700404 playbyplay http stats nba com game 0041700404 playbyplay 我想创建一个表格 其中包
  • HTTR GET 新错误:SSL 证书问题:证书已过期

    我已经运行这段代码几个月了 没有出现任何问题 今天我突然开始在我的两台 AWS 服务器上收到以下错误消息 错误 curl curl fetch memory url handle handle SSL证书问题 证书已过期 当尝试运行以下代码
  • 无法将“gather”输出的列名称更改为默认名称以外的任何名称

    我正在尝试使用gather in the tidyr包 但我无法更改默认名称的输出列名称 例如 df data frame time 1 100 a 1 100 b 101 200 df long df gt gather foo bar
  • 在 RGL 中将立方体绘制到 3D 散点图中

    我正在尝试向 3D 散点图添加较小的立方体 网格 具有指定边长 我希望立方体位于原点 我该怎么做呢 我已经玩过cube3d 但我似乎无法将立方体正确定位 也无法使其成为网格 因此我可以看到它包含的数据点 这是我所拥有的 library rg
  • 将 Excel 文件读入 R 并锁定单元格

    我有一个 Excel 电子表格要读入 R 它受密码保护并锁定了单元格 我可以使用 excel link 导入受密码保护的文件 但我不知道如何解锁 取消保护单元格 excel link 给了我这个错误 gt
  • 如何在R中实现countifs函数(excel)

    我有一个包含 100000 行数据的数据集 我尝试做一些countifExcel 中的操作 但速度慢得惊人 所以我想知道R中是否可以完成这种操作 基本上 我想根据多个条件进行计数 例如 我可以指望职业和性别 row sex occupati
  • 如何根据多个条件创建列?

    我有一个数据框 我想根据多个条件创建一个列 v1 v2 v3 v4 v5 4 1 2 4 5 5 5 2 4 5 6 21 9 20 30 50 6 4 5 7 9 10 3 6 5 9 基本上 使用以下可能的值创建 v6 Cat dog
  • R 中图周围的圆形边框

    我需要在情节周围放置平滑的边框 代码 plot 0 10 0 10 type n xlab X ylab Y box figure col blue 除了简单的蓝线 我如何放置带有圆角的平滑灰线 非常感谢 library grid plot
  • 计算字符串向量中连续数字的函数

    我想创建一个函数 它接受至少 1 个元素的字符串对象并包含数字 2 到 5 并确定是否存在至少 N 长度的连续数字 其中 N 是实际数字值 如果是 则返回字符串 true 否则返回字符串 false 例如 Input 555123 Outp
  • 识别包含字符串的行的最快方法[重复]

    这个问题在这里已经有答案了 我有一个字符串数据框 尺寸为 30 列 x 500 万行 我想识别包含任何预定义字符串列表的行 有没有比下面我的 apply any 方法更快的方法 这是一个可重现的示例 请注意 此示例中的字符串是随机数 但在我
  • rpart 决策树中的 rel 误差和 x 误差有什么区别? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个来自 UCI 机器学习数据库的纯分类数据框https archive ics uci edu ml datasets Diabet
  • rPlot 工具提示问题

    我有一个使用 rCharts 工具提示的简单示例 但似乎不起作用 set seed 1 test lt data frame x rnorm 100 y rnorm 100 rPlot y x data test type point to
  • 通过消除嵌套的 for 循环来改进此代码

    R 包corrplot除其他内容外 还包含这个漂亮的功能 cor mtest lt function mat conf level 0 95 mat lt as matrix mat n lt ncol mat p mat lt lowCI
  • autoplot.microbenchmark 实际绘制了什么?

    根据文档 microbenchmark autoplot 使用 ggplot2 生成更清晰的微基准计时图 凉爽的 让我们尝试一下示例代码 library ggplot2 tm lt microbenchmark rchisq 100 0 r
  • 函数速度测试的奇怪结果

    我编写了一个使用递归来查找最大公因数 分母 的函数 gt gcd function a b if length a length b gt 1 warning Only scalars allowed using first element

随机推荐