我试图稍微清理我的代码。目前,我使用不同的参数组合多次运行rollapplyr
。以下是我使用它的功能:
library('assertthat')
var_sums <- function(x, sign) {
assert_that(length(x) > 0)
if (sign == "pos") {
sum(x[which(x > 0)])
} else if (sign == "neg") {
sum(x[which(x < 0)])
} else {
stop("variable sign must have value of 'pos' or 'neg'")
}
}
它采用-1和1之间的正数和负数的向量x
,并且根据sign
的值将正数或仅负数相加。目前我通过以下方式实施了它:
library('zoo')
set.seed(1)
vec <- round(runif(100, min = -1), digits = 2)
pos_4 <- rollapplyr(vec, 4, var_sums, sign = "pos", fill = NA)
neg_4 <- rollapplyr(vec, 4, var_sums, sign = "neg", fill = NA)
pos_12 <- rollapplyr(vec, 12, var_sums, sign = "pos", fill = NA)
neg_12 <- rollapplyr(vec, 12, var_sums, sign = "neg", fill = NA)
y <- data.frame(pos_4, neg_4, pos_12, neg_12)
当然,使用do.call
或mapply
或其他东西,我只是没有看到更优雅的方式。
答案 0 :(得分:2)
searchBar.becomeFirstResponder()
searchBar.removeLayerAnimationsRecursively()