R 带有ifelse和两个对象的自定义函数需要以动态方式从正确的列中读取
如何确保对象中两列的数据(数据) 通过自定义函数以动态方式查看另一个对象(B_)的相应列 见下例:R 带有ifelse和两个对象的自定义函数需要以动态方式从正确的列中读取,r,function,object,if-statement,xts,R,Function,Object,If Statement,Xts,如何确保对象中两列的数据(数据) 通过自定义函数以动态方式查看另一个对象(B_)的相应列 见下例: # example data require(xts) set.seed(3) A <- matrix(runif(18, max=9), ncol=2) Data <- xts(A, Sys.Date()-9:1) names(Data) <- c("C1", "C2") 对于这些数据,我使用分位数(底部20%和顶部20%): B_ C1
# example data
require(xts)
set.seed(3)
A <- matrix(runif(18, max=9), ncol=2)
Data <- xts(A, Sys.Date()-9:1)
names(Data) <- c("C1", "C2")
对于这些数据,我使用分位数(底部20%和顶部20%):
B_
C1 C2
5.427162 6.786868
创建函数:
# function to test whether data is bigger than the quantile
test_a <- function(x,l,u)
{
for (i in 1:(nrow(x)))
for (j in 1:(ncol(x)))
b <- ifelse(x > u[j] , 1, 0)
return(b)
}
其中2016-05-03的C1给出了不正确的结果,因为它应该是一个。当我显式调用该列时,可以从下一个结果中看到这一点
# calling the function with explicit columns)
# If I explicit the column to look into, it works.
# However I have 100 columns of data so that won't work
result_correct <- test_a(Data[,1], B_l[1], B_u[1])
C1
2016-04-28 0
2016-04-29 1
2016-04-30 0
2016-05-01 0
2016-05-02 0
2016-05-03 1
2016-05-04 0
2016-05-05 0
2016-05-06 0
#使用显式列调用函数)
#如果我明确了要查看的列,它就会工作。
#但是,我有100列数据,所以这不起作用
result_correct由于OP没有在自定义函数中使用,因此不清楚函数中应在何处使用“B_l”。除此之外,如果“Data”的列名与“B_”的顺序不同,我们可以通过在“Data”中指定“B_”的名称作为列索引,即数据[,名称(B_)]
来实现相同的顺序。接下来要纠正的是使“B_”和“Data”的长度相同。我们通过按“数据”中的行数复制“B_”的每一列来实现这一点。在这里,我们使用col
获取“Data”的列索引来进行复制。由于长度现在相等,我们只需做
就可以得到一个逻辑矩阵,我们通过0求和将其强制为二进制
test_a <- function(x, l, u){
(x[, names(u)] > u[col(x[, names(u)])]) + 0
}
test_a(Data, B_l, B_u)
# C1 C2
#2016-04-28 0 0
#2016-04-29 1 0
#2016-04-30 0 0
#2016-05-01 0 0
#2016-05-02 0 0
#2016-05-03 1 1
#2016-05-04 0 1
#2016-05-05 0 0
#2016-05-06 0 0
test_a u[col(x[,names(u)])]+0
}
测试a(数据、B_l、B_)
#C1 C2
#2016-04-28 0 0
#2016-04-29 1 0
#2016-04-30 0 0
#2016-05-01 0 0
#2016-05-02 0 0
#2016-05-03 1 1
#2016-05-04 0 1
#2016-05-05 0 0
#2016-05-06 0 0
在函数中,您没有使用l
,那么为什么这是测试a
中的一个参数呢?因为实际上,在另一个测试中,我查找2个分位数(u&l)之间的数据。我刚刚用你的建议测试了cl[col(x[,names(l)])]+0&(x[,names(u)]# function to test whether data is bigger than the quantile
test_a <- function(x,l,u)
{
for (i in 1:(nrow(x)))
for (j in 1:(ncol(x)))
b <- ifelse(x > u[j] , 1, 0)
return(b)
}
# calling the function (dynamic)
# How can i make sure the data of both columns in Data
# look in the corresponding columns of B_u?
result_wrong <- test_a(Data, B_l, B_u)
C1 C2
2016-04-28 0 0
2016-04-29 1 0
2016-04-30 0 0
2016-05-01 0 0
2016-05-02 0 0
2016-05-03 0 1
2016-05-04 0 1
2016-05-05 0 0
2016-05-06 0 0
# calling the function with explicit columns)
# If I explicit the column to look into, it works.
# However I have 100 columns of data so that won't work
result_correct <- test_a(Data[,1], B_l[1], B_u[1])
C1
2016-04-28 0
2016-04-29 1
2016-04-30 0
2016-05-01 0
2016-05-02 0
2016-05-03 1
2016-05-04 0
2016-05-05 0
2016-05-06 0
test_a <- function(x, l, u){
(x[, names(u)] > u[col(x[, names(u)])]) + 0
}
test_a(Data, B_l, B_u)
# C1 C2
#2016-04-28 0 0
#2016-04-29 1 0
#2016-04-30 0 0
#2016-05-01 0 0
#2016-05-02 0 0
#2016-05-03 1 1
#2016-05-04 0 1
#2016-05-05 0 0
#2016-05-06 0 0