r - 为 R 中的每个组找到 75 个百分位数并用中位数替换
问题描述
这些问题与我自己的主题 计算 90% 并用 R 中的组中位数替换它类似
有了这个区别。
但是,在该主题中,请注意,计算是由一类动作之前的 14 个零完成的,但对于所有零类动作并用中位数替换,并为每个组代码+项目执行
即,现在我使用全零而不是前面的 14,并且不要触及返回的负值和零值。
通过类别的组变量(动作- 0、1)Zero
,我想通过返回变量找到 75 个百分位,如果值超过 75 个百分位,则必须按zero
类别将其替换为中位数。所以有code
变量这个过程必须对代码单独执行。注意:我不接触负值和零值
mydat=structure(list(code = c(123L, 123L, 123L, 123L, 123L, 123L, 123L,
123L, 123L, 123L, 123L, 123L, 124L, 124L, 124L, 124L, 124L, 124L,
124L, 124L, 124L, 124L, 124L, 124L), action = c(0L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L), return = c(-1L, 0L, 23L, 100L, 18L, 15L, -1L,
0L, 23L, 100L, 18L, 15L, -1L, 0L, 23L, 100L, 18L, 15L, -1L, 0L,
23L, 100L, 18L, 15L)), .Names = c("code", "action", "return"), class = "data.frame", row.names = c(NA,
-24L))
\
23
100
18
15
如何获得该输出。所以 75 个百分位:
42,25 中位数=20,5 替换
add action return
123 0 -1
123 0 0
123 0 23
123 0 ***20,5
123 0 18
123 0 15
123 1 -1
123 1 0
123 1 23
123 1 100
123 1 18
123 1 15
124 0 -1
124 0 0
124 0 23
124 0 ***20,5
124 0 18
124 0 15
124 1 -1
124 1 0
124 1 23
124 1 100
124 1 18
124 1 15
使用最好的 Uwe 解决方案,我得到了错误
Error in `[.data.table`(mydat[action == 0, `:=`(output, as.double(return))], :
Column(s) [action] not found in i
如何做到我不接触的负值和零值以及发生此错误的原因。
library(data.table)
# mark the zero acton rows before the the action period
setDT(mydat)[, zero_before := cummax(action), by = .(code)]
# compute median and 90% quantile for that last 14 rows before each action period
agg <- mydat[zero_before == 0,
quantile(tail(return), c(0.5, 0.75)) %>%
as.list() %>%
set_names(c("med", "q90")) %>%
c(.(zero_before = 0)), by = .(code)]
agg
# append output column
mydat[action == 0, output := as.double(return)][
# replace output values greater q90 in an update non-equi join
agg, on = .(code,action, return > q90), output := as.double(med)][
# remove helper column
, zero_before := NULL]
解决方案
return
如果我理解正确,则 OP 希望根据返回大于 0 的所有零操作行计算每个组内的中位数和 75% 分位数。然后,零操作行中超过 75% 分位数的任何返回值相应的组将由组中位数代替。
代码可以大大简化,因为我们不必区分动作行之前和之后的零动作行。
下面的代码重现了预期的结果:
library(data.table)
library(magrittr)
# compute median and 90% quantile for that last 14 rows before each action period
agg <- setDT(mydat)[action == 0 & return > 0,
quantile(return, c(0.5, 0.75)) %>%
as.list() %>%
set_names(c("med", "q75")), by = .(code, action)]
# append output column
mydat[, output := as.double(return)][
# replace output values greater q75 in an update non-equi join
agg, on = .(code, action, return > q75), output := as.double(med)]
mydat[]
code action return output 1: 123 0 -1 -1.0 2: 123 0 0 0.0 3: 123 0 23 23.0 4: 123 0 100 20.5 5: 123 0 18 18.0 6: 123 0 15 15.0 7: 123 1 -1 -1.0 8: 123 1 0 0.0 9: 123 1 23 23.0 10: 123 1 100 100.0 11: 123 1 18 18.0 12: 123 1 15 15.0 13: 124 0 -1 -1.0 14: 124 0 0 0.0 15: 124 0 23 23.0 16: 124 0 100 20.5 17: 124 0 18 18.0 18: 124 0 15 15.0 19: 124 1 -1 -1.0 20: 124 1 0 0.0 21: 124 1 23 23.0 22: 124 1 100 100.0 23: 124 1 18 18.0 24: 124 1 15 15.0 code action return output
推荐阅读
- flutter - 运行项目时 Flutter vscode 错误任务':app:checkDebugAarMetadata'执行失败
- python - 变量没有从函数返回正确的值
- python - 这个 BFS 实现是否正确?
- reactjs - 像这样布局顶级网页的组件
- c++ - 如何让这个程序重复,直到用户决定终止?
- elasticsearch - 如何在 ElasticSearch 上按当前时间过滤 integer_range 字段?
- javascript - 节点使用ObjectionJS和Knex,1对多,从many表中返回第一个相关行
- arduino - 从 AsyncUDP onPacket 回调中使用 libsodium 解密时触发 ESP32 堆栈金丝雀观察点
- flutter - 当 itemExtent != null 时 ReorderableList 的奇怪行为
- deep-learning - 锚箱解释