更新时间:2023-11-22 23:15:10
如果我们假设配对点列表,并且您可以相信这些值是有效的(可能不是一个合理的假设,但是可以添加防御措施),看来您可能只需使用 min
和 max
来做到这一点:
If we assume a list of paired points, and that you can trust the values are valid (probably not a fair assumption, but defenses could be added), it seems that you may be able to just do this with min
and max
:
inputData <-
list(
c(1,10)
, c(7,3)
, c(4,5)
)
lapply(inputData, function(x){
c(max(x), min(x))
})
给予
[[1]]
[1] 10 1
[[2]]
[1] 7 3
[[3]]
[1] 5 4
如果转换为矩阵,则可以从向量获得相同的基本输出/data.frame,然后使用具有相同功能的 apply
按行。矩阵方法可能类似于:
You can get the same basic output from vectors, if you convert to a matrix/data.frame, then use apply
by row with the same function. The matrix approach likely looks something like:
matData <-
do.call(rbind, inputData)
t(apply(matData, 1, function(x){
c(max(x), min(x))
}))
并给出:
[,1] [,2]
[1,] 10 1
[2,] 7 3
[3,] 5 4
鉴于更新,我不得不说我同意@Frank,您应该对此处的内容有基本了解,但请使用 pmax
和 pmin
。然后,该函数如下所示(请注意,我将 x
和 y
设置为参数,因为它们似乎是必需的)。
Given the update, I have to say that I agree with @Frank that you should take the basic idea of what I have here, but use pmax
and pmin
instead. The function then looks like this (note, I am setting x
and y
as arguments, since they appear to be required).
cb <- function(corrPlot, x, y, rectArgs = list() ){
# ... pass named vector of x and y names
# for upper x > y, lower x < y
useX <- pmax(x, y)
useY <- pmin(x,y)
n <- ncol(corrPlot)
nms <- colnames(corrPlot)
colnames(corrPlot) <- if(is.null(nms)) 1:ncol(corrPlot) else nms
xleft <- match(useX, colnames(corrPlot)) - 0.5
ybottom <- n - match(useY, colnames(corrPlot)) + 0.5
lst <- list(xleft=xleft, ybottom=ybottom, xright=xleft+1, ytop=ybottom+1)
do.call(rect, c(lst, rectArgs))
}
然后,这似乎按预期工作:
Then, this appears to work as expected:
cb(plt, x=c(1, 3, 4), y=c(10, 7, 5), rectArgs=list(border="red", lwd=3))
正如预期的那样,有三种生成所需对排序的方法,具体取决于它们是成对列表,矩阵/data.frame还是a向量。另一种方法是接受列表或matrix / data.frame作为函数的参数,然后使用 lapply
或 apply
函数,并从中提取所需的值。
As expected, there are three ways to generate the desired pair ordering, depending on whether they come in a list of pairs, a matrix/data.frame, or a vector. The alternative approach would be to accept either a list or a matrix/data.frame as an argument to the function, then use the lapply
or apply
functions above and extract your required values from those.