具有双重重置的复数累加和

2024-04-16

我试图遵循一些关于何时将数据分组到图表中的规则。 我将如何处理这个数据框:

# A tibble: 11 x 8
   assay      year   qtr invalid valid total_assays    hfr predicted_inv
   <chr>     <dbl> <dbl>   <dbl> <dbl>        <dbl>  <dbl>         <dbl>
 1 test_case 2016.    1.      2.   36.          38. 0.0350         1.33 
 2 test_case 2016.    2.      1.   34.          35. 0.0350         1.23 
 3 test_case 2016.    3.      0.   25.          25. 0.0350         0.875
 4 test_case 2016.    4.      2.   23.          25. 0.0350         0.875
 5 test_case 2017.    1.      1.   29.          30. 0.0350         1.05 
 6 test_case 2017.    2.      2.   24.          26. 0.0350         0.910
 7 test_case 2017.    3.      0.   23.          23. 0.0350         0.805
 8 test_case 2017.    4.      1.   20.          21. 0.0350         0.735
 9 test_case 2018.    1.      2.   33.          35. 0.0350         1.23 
10 test_case 2018.    2.      5.   28.          33. 0.0350         1.16 
11 test_case 2018.    3.      4.    9.          13. 0.0350         0.455

对于这个:

       assay year qtr invalid valid total_assays   hfr predicted_inv co_inv co_val co_prd_inv trend
1  test_case 2016   1       2    36           38 0.035         1.330      2     36      1.330    No
2  test_case 2016   2       1    34           35 0.035         1.225      3     70      2.555    No
3  test_case 2016   3       0    25           25 0.035         0.875      3     95      3.430    No
4  test_case 2016   4       2    23           25 0.035         0.875      5    118      4.305   Yes
5  test_case 2017   1       1    29           30 0.035         1.050      1     29      1.050    No
6  test_case 2017   2       2    24           26 0.035         0.910      3     53      1.960    No
7  test_case 2017   3       0    23           23 0.035         0.805      3     76      2.765    No
8  test_case 2017   4       1    20           21 0.035         0.735      4     96      3.500    No
9  test_case 2018   1       2    33           35 0.035         1.225      6    129      4.725   Yes
10 test_case 2018   2       5    28           33 0.035         1.155      5     28      1.155   Yes
11 test_case 2018   3       4     9           13 0.035         0.455      4      9      0.455    No

规则相当简单。对于每一行,如果 invalid 或 Predicted_inv 的累积和为 5 或更大,则趋势为“yes”,并且所有三个参数(invalid、valid、predicted_inv)的累积和都将重置并从下一行重新开始。最后,分组 (co_*) 将呈现趋势。

我尝试过使用 dplyr 的一些解决方案,但是当我尝试同时创建多个相互依赖的变量时,我不断收到错误。

现在我正在尝试一个自定义函数,它仅将 3 个参数作为向量,但我一直被迫构建循环......我更喜欢一个易于阅读的 dplyr 解决方案。

这是 dputs:

egdf1 <- structure(list(assay = c("test_case", "test_case", "test_case", 
                         "test_case", "test_case", "test_case", "test_case", "test_case", 
                         "test_case", "test_case", "test_case"), year = c(2016, 2016, 
                                                                          2016, 2016, 2017, 2017, 2017, 2017, 2018, 2018, 2018), qtr = c(1, 
                                                                                                                                         2, 3, 4, 1, 2, 3, 4, 1, 2, 3), invalid = c(2, 1, 0, 2, 1, 2, 
                                                                                                                                                                                    0, 1, 2, 5, 4), valid = c(36, 34, 25, 23, 29, 24, 23, 20, 33, 
                                                                                                                                                                                                              28, 9), total_assays = c(38, 35, 25, 25, 30, 26, 23, 21, 35, 
                                                                                                                                                                                                                                       33, 13), hfr = c(0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 
                                                                                                                                                                                                                                                        0.035, 0.035, 0.035, 0.035), predicted_inv = c(1.33, 1.225, 0.875, 
                                                                                                                                                                                                                                                                                                       0.875, 1.05, 0.91, 0.805, 0.735, 1.225, 1.155, 0.455)), .Names = c("assay", 
                                                                                                                                                                                                                                                                                                                                                                          "year", "qtr", "invalid", "valid", "total_assays", "hfr", "predicted_inv"
                                                                                                                                                                                                                                                                                                       ), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                  -11L))

egdf2 <- structure(list(assay = c("test_case", "test_case", "test_case", 
                         "test_case", "test_case", "test_case", "test_case", "test_case", 
                         "test_case", "test_case", "test_case"), year = c(2016L, 2016L, 
                                                                          2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L
                         ), qtr = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L), invalid = c(2L, 
                                                                                             1L, 0L, 2L, 1L, 2L, 0L, 1L, 2L, 5L, 4L), valid = c(36L, 34L, 
                                                                                                                                                25L, 23L, 29L, 24L, 23L, 20L, 33L, 28L, 9L), total_assays = c(38L, 
                                                                                                                                                                                                              35L, 25L, 25L, 30L, 26L, 23L, 21L, 35L, 33L, 13L), hfr = c(0.035, 
                                                                                                                                                                                                                                                                         0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 0.035, 
                                                                                                                                                                                                                                                                         0.035), predicted_inv = c(1.33, 1.225, 0.875, 0.875, 1.05, 0.91, 
                                                                                                                                                                                                                                                                                                   0.805, 0.735, 1.225, 1.155, 0.455), co_inv = c(2L, 3L, 3L, 5L, 
                                                                                                                                                                                                                                                                                                                                                  1L, 3L, 3L, 4L, 6L, 5L, 4L), co_val = c(36L, 70L, 95L, 118L, 
                                                                                                                                                                                                                                                                                                                                                                                          29L, 53L, 76L, 96L, 129L, 28L, 9L), co_prd_inv = c(1.33, 2.555, 
                                                                                                                                                                                                                                                                                                                                                                                                                                             3.43, 4.305, 1.05, 1.96, 2.765, 3.5, 4.725, 1.155, 0.455), trend = c("No", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  "No", "No", "Yes", "No", "No", "No", "No", "Yes", "Yes", "No"
                                                                                                                                                                                                                                                                                                                                                                                                                                             )), .Names = c("assay", "year", "qtr", "invalid", "valid", "total_assays", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                            "hfr", "predicted_inv", "co_inv", "co_val", "co_prd_inv", "trend"
                                                                                                                                                                                                                                                                                                                                                                                                                                         ), class = "data.frame", row.names = c(NA, -11L))

使用功能cumsumbinning从 MESS 包中设置累积组总和不得超过的阈值(示例中为 5)。请记住,在第 9 行中,因为 2 与 4 相加超过了 5 的阈值,所以会创建另一个组,而在您所需的输出中,您希望在下一行中重置该组。

library(MESS)  
  egdf1 %>%
  group_by(group = cumsumbinning(invalid, 5)) %>%
  mutate(
    co_inv = cumsum(invalid),
    co_val = cumsum(valid),
    co_prd_inv = cumsum(predicted_inv),
    trend = ifelse(group - lag(group, default = 0) > 1, "yes", "no")
  )

Output

   assay      year   qtr invalid valid total_assays   hfr predicted_inv group co_inv co_val co_prd_inv trend
   <chr>     <dbl> <dbl>   <dbl> <dbl>        <dbl> <dbl>         <dbl> <int>  <dbl>  <dbl>      <dbl> <chr>
 1 test_case  2016     1       2    36           38 0.035         1.33      1      2     36      1.33  no   
 2 test_case  2016     2       1    34           35 0.035         1.23      1      3     70      2.56  no   
 3 test_case  2016     3       0    25           25 0.035         0.875     1      3     95      3.43  no   
 4 test_case  2016     4       2    23           25 0.035         0.875     1      5    118      4.30  no   
 5 test_case  2017     1       1    29           30 0.035         1.05      2      1     29      1.05  yes  
 6 test_case  2017     2       2    24           26 0.035         0.91      2      3     53      1.96  no   
 7 test_case  2017     3       0    23           23 0.035         0.805     2      3     76      2.76  no   
 8 test_case  2017     4       1    20           21 0.035         0.735     2      4     96      3.5   no   
 9 test_case  2018     1       2    33           35 0.035         1.23      3      2     33      1.23  yes  
10 test_case  2018     2       5    28           33 0.035         1.16      4      5     28      1.16  yes  
11 test_case  2018     3       4     9           13 0.035         0.455     5      4      9      0.455 yes 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有双重重置的复数累加和 的相关文章

随机推荐

  • 实体框架 Fluent API 映射简单的一对多关系

    我有两张桌子 文档 Id DocumentTypeId 标题 详细信息 文档类型 ID 名称 描述 DocumentTypeId 是引用 DocumentTypes 表的外键 IE 所有文件都可以应该 有一个分配给它们的类型 我有两节课 p
  • 多人台球游戏物理模拟[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 将邻接矩阵转换为 Cytoscape 的 Edgelist(csv 文件)

    我的 csv 文件中有一个大的 200 列 行 邻接矩阵 这详细说明了个体之间的互动 我想将此文件转换为边缘列表 可以手动完成 但需要大量时间 下面显示了一小部分数据 第一个单元格是空格 A B C A 0 0 1 B 0 0 1 C 1
  • (0, _reactI18next.translate) 不是函数

    实际上 我是本地反应新手 在这里我尝试使用 react i18next 将语言更改为阿拉伯语 但在执行时出现以下错误 ReactNativeJS 0 reactI18next translate 不是一个函数 在 0 reactI18nex
  • AppDelegate 或 AppController

    在阅读可可教程时 我注意到一些教程使用AppDelegate还有一些AppController用于定义IBActions打开使用子类的各种窗口NSWindowController 这有某种经验法则吗 我创建一个仅是我的应用程序委托的类 并实
  • 如何使用 Visual Studio 2008 对 C# Web 服务进行单元测试

    您应该如何使用 Visual Studio 2008 对 C 中的 Web 服务进行单元测试 当我生成单元测试时 它会添加对 Web 服务类的实际引用 而不是 Web 引用 它设置以下中指定的属性 http msdn microsoft c
  • 检查 netcdf linux 库版本

    如何确定我的系统中安装了哪个版本的 netcdf 库 有命令行吗 我尝试搜索 netcdf 发现了一堆文件 但无法确定版本号 有没有命令可以检查已安装的任何版本 我在ubuntu上 netCDF 提供nc config用于此目的的命令行工具
  • 在 Rust 中应该如何进行指针算术?

    我知道答案是 你不应该 但为了争论 如何should你做吧 例如 如果您想编写一个替代方案Vec
  • Apache 服务器上的 React、js

    我正在一个react js项目中工作 我有一个安装了apache服务器的云服务器 我的问题是我可以在 apache 服务器上设置我的反应项目吗 正如达文 泰伦所说 react是一种浏览器技术 除了客户端浏览器从服务器下载应用程序之外 一切都
  • 用户模型中带有 uuid 列的 Laravel Sanctum 不保存 tokenable_id

    我尝试使用Laravel 8 x and Laravel sanctum 2 14 2验证我的 API 和 UUID 作为我的主密钥User model 我的定制PersonalAccessToken model use Illuminat
  • 在 Google Places Apis 中搜索特定城市内的位置

    我正在使用 Google Places Apis 来过滤特定城市内的结果 我能够过滤结果 但它也会显示该城市之外的结果 例如 如果我设置德里市的 LatLngBounds 并搜索纽约市的位置 它还给了我纽约市的结果 但纽约的 LatLng
  • 为什么要实现 IEquatable 接口

    我一直在阅读文章并在一定程度上理解接口 但是 如果我想纠正我自己的自定义 Equals 方法 似乎我可以在不实现 IEquatable 接口的情况下做到这一点 一个例子 using System using System Collectio
  • Python 如何在一行中分配多个变量?

    Python 在一行中分配多个变量实际上执行了哪些步骤 我以前经常做 A 0 A 1 A 1 A 0 来交换 但是最近在分配链表时遇到了一个错误 insert self gt node gt def insert next self nod
  • Spark中连接两个RDD

    我有两个 rdd 一个 rdd 只有一列 其他有两列来连接键上的两个 RDD 我添加了虚拟值 0 是否有其他有效的方法可以使用 join 来执行此操作 val lines sc textFile ml 100k u data val mov
  • conda 内部是如何工作的?

    我搜索了一段时间但找不到满意的答案 康达 http conda pydata org http conda pydata org 在内部工作 任何细节欢迎 此外 由于它与 python 无关并且显然工作得如此良好和流畅 为什么它不被用作像
  • Spring Boot - NoClassDefFoundError:ch/qos/logback/classic/Level

    我创建了一个普通的 Spring Boot 应用程序 1 5 9 RELEASE 但是当我Run As gt Spring Boot App 在 Eclipse Oxygen 中 我明白 SLF4J Failed to load class
  • 在 R Markdown 中将数据框显示为表格

    In knitr我想使用 kable 包添加一个 小 数据框作为表格 output html document r knitr kable mtcars 1 5 1 5 format html 这将返回一个如上所述的紧凑表 同时将其更改为f
  • 机器人框架 - 清除元素文本关键字不起作用

    我们有一个 html 结构的文本字段 如下所示
  • 在 CSS 流布局中自动调整图像大小以模拟 html 表格布局

    我有一个图像 根据屏幕分辨率 它会在 CSS 流布局中下降到看不见的位置 因为我已将其宽度和高度设置为静态值 CSS 流布局中是否有一种方法可以在有人缩小浏览器窗口时自动调整图像大小 我已经在 html table 布局中看到了这一点 并且
  • 具有双重重置的复数累加和

    我试图遵循一些关于何时将数据分组到图表中的规则 我将如何处理这个数据框 A tibble 11 x 8 assay year qtr invalid valid total assays hfr predicted inv