我一直在尝试写一个代码,检查如果条件a[i-1]和a[i],将更新b[i]与b[i-1]和c[i]的值,如果条件失败,那么b[i]必须更新为0
我现在的代码是:
#R
library(dplyr)
update_b <- function(data) {
for (i in 2:nrow(data)) {
if (!is.na(data$a[i]) & !is.na(data$a[i-1]) & data$a[i] < 60 & data$a[i-1] < 60) {
data$b[i] <- data$c[i] + data$b[i-1]
} else {
data$b[i] <- 0
}
}
return(data)
}
result <- data_frame %>%
group_by(number) %>%
arrange(date) %>%
do(update_b(.))
字符串
它一直运行到:
|============= | 13% \~40 s remaining
Error in `$<-`:
! Assigned data `*vtmp*` must be compatible with existing data.
x Existing data has 1 row.
x Assigned data has 2 rows.
i Row updates require a list value. Do you need `list()` or `as.list()`?
Caused by error in `vectbl_recycle_rhs_rows()`:
! Can't recycle input of size 2 to size 1.
型
之前我一直在尝试使用data.table来解决这个问题:
#R
library(data.table)
calculate_b <- function(x) {
for (i in 2:nrow(x)) {
if (x[i, a] < 60 & x[i - 1, a] < 60) {
x[i, b:= x[i, c] + x[i - 1, b]]
} else {
x[i, b:= 0]
}
}
return(x)
}
a[, b:= 0]
a <- a[, calculate_b(.SD), by = number]
型
给了我一个错误
.SD is locked. Using := in .SD's j is reserved for possible future use; a tortuously flexible way to modify by group. Use := in j directly to modify by group by reference.
型
如何解决这个错误?
编辑:这是数据样本
| ID(编号)|一|C| B(开始时设置为0)|
| --|--|--|--|
| 123 | 30 | 0 | 0 |
| 123 | 25 | 45 | 45 |
| 123 | 18 | 8 | 53 |
| 123 | 80 | 15 | 0 |
| 123 | 45 | 63 | 0 |
| 123 | 15 | 75 | 75 |
| 123 | 70 | 12 | 0 |
| 456 | 65 | 0 | 0 |
| 456 | 45 | 75 | 0 |
| 456 | 30 | 26 | 26 |
| 456 | 58 | 95 | 121 |
| 456 | 53 | 41 | 162 |
| 456 | 50 | 32 | 194 |
| 789 | 45 | 0 | 0 |
| 789 | 90 | 14 | 0 |
| 789 | 89 | 65 | 0 |
| 789 | 75 | 78 | 0 |
| 789 | 80 | 59 | 0 |
| 789 | 50 | 32 | 0 |
1条答案
按热度按时间ugmeyewa1#
尝试#3:-)
我们将预先计算
b
应该被重置为0的点(称之为reset
),然后通过这个reset
变量的召唤重复(使用data.table::rleid
)进行分组。字符串
我在输出中保留了
reset
和grp
,只是为了显示它们的值,只需用DT[, c("reset","grp") := NULL]
删除它们。数据来自您的问题,为简单起见已重命名:
型