- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试遵循一些关于何时将数据分组以绘制图表的规则。我将如何离开这个数据框:
# 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 或更大,则趋势为"is",所有三个参数(无效、有效、predicted_inv)的累积和将重置并从下一行重新开始。最后,分组 (co_*) 将被趋势化。
我尝试了一些使用 dplyr 的解决方案,但是当我尝试同时创建多个相互依赖的变量时,我总是遇到错误。
现在我正在尝试一个仅将 3 个参数作为向量的自定义函数,但我一直被迫构建循环...我更喜欢易于阅读的 dplyr 解决方案。
这里是输出:
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))
最佳答案
使用 MESS 包中的函数 cumsumbinning
设置累积组总和不得超过的阈值(在您的示例中为 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")
)
输出
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
关于r - 具有双重重置的复数累加和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936638/
我有一个包含两列的文件,第一列包含纯实数,第二列包含 GNUPLOT 可接受的形式的复数,{a,b}(GNUPLOT 等同于 a + bi)。我想用第二列的绝对值绘制第一列,所以我尝试了这个: 使用
我正在使用 pandas,它可以非常有效地按照我需要的方式对数据进行排序/过滤。 此代码运行良好,直到我将最后一列更改为复数;现在我得到一个错误。 return self._cython_agg_ge
我如何让 Django 意识到 countries 的单数形式是 country 而不是 countrie 最佳答案 来自docs ,如果你有一个名为 num_countries 的模板变量,你可以这
我正在从事一个项目,该项目要求我获取输入、执行 DFT(离散傅立叶变换),然后从这些值中获取过零次数。 我编写了一个算法,但它使用复数,我不知道如何操作/执行计算。这是代码: #include #i
我如何在 C 中动态定义复数的三维数组,以便我可以在访问数组时方便的 [i] [j] [k] 符号中访问? 最佳答案 扩展 Bob__ 的 example将数组分配到堆上而不是堆栈上: #includ
我想在我的 Android 项目中使用复数形式。但是,我提供的值可以是浮点值。 例如,当设置 1.5 星时,我希望这个理解,它不是 1 星而是 1.5 星s。 %d star %d star
问题:这是我的虚数正则表达式: [0-9]{0,}\d\.[0-9]{0,}\d[i]|[0-9]{0,}\d[i] 它只取一个纯复数部分的虚数。 当我的解析器遇到例如String im = "2i"
我知道关键,现在我需要搜索 5000 多个用户数据库时产生的所有 结果。任何用户可能没有一个或多个位置,由 id 和 name 字段标识。因此,我需要一个数组中的结果,不仅仅是第一个/最后一个,而是所
Gidday cobbers/尊敬的同事们, 通过实现卡尔曼预测/校正的多目标跟踪,我在其他 SO 线程中看到建议的一般方法是简单地为每个目标提供一个卡尔曼滤波器 vector/数组。 即'多单目标卡
尽管在 C# naming convention for enum and matching property 上阅读了类似但不完全是我想要的内容,但我还是在问这个问题 我发现我倾向于以复数命名枚举,
我正在尝试使用 getQuantityString基于 Android 开发人员指南检索数量字符串(复数)的资源中的方法 Quantity string (plurals) 我得到的错误是 Error
我想用 C++ 处理复数。因此我包括了#include .现在我的问题是:我如何声明一个变量?(那么我们说的格式是什么:1 + i?) 提前致谢:-) 最佳答案 // 1 + 2i std::com
我有一个名为 agency_persons 的表,其中已有一些数据。我使用 php artisan 创建了一个模型: php artisan make:model AgencyPerson 现在,当我
在 C++ 中,复数 double 的一维数组的最大大小是多少(即 std::complex array[dimension] )? 如果我在 main 和 global 中声明数组,最大大小是否有区
我使用 Mustache 来模板化我的 javascript ajax 调用,这是我的数据和模板: {'joined':1} // ajax responde data json. var myTem
我有这么大的问题。我写了一个关于复数的程序。该程序读取和写入复数,将它们相加等。 他说我应该读一下 Why should I overload a C++ operator as a global f
我将在 java 中重复我必须做的事情,以我认为我需要思考的方式来完成这个任务。 (抱歉,我是编程新手)。 头等舱;为复数定义类。我发现这很容易,我的答案如下。 public class Comple
如何在新的 dev django 版本的管理站点上将某些模型名称从“Categorys”更改为“Categories”?在旧版本中(没有管理站点和管理模型)你可以这样做; http://www.the
我最近才发现一种通过 this video 生成毕达哥拉斯三元组的方法解释它,涉及使用高斯(复杂)整数。到目前为止,我已经设法编写了一个函数,该函数返回由每个高斯整数生成的勾股三元组列表,其中虚部小于
我决定尝试一下 complex.h,然后遇到了一个我认为非常奇怪的问题。 int mandelbrot(long double complex c, int lim) { long doubl
我是一名优秀的程序员,十分优秀!