本文介绍了如何在R中将对翻转为X> Y顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户给出了一对ID,分别对应于Y> X时上三角中的ID。
我在想如何处理这些点以及哪种数据结构最好。
我认为具有(x1,y1),(y3,x3),... 这样的对很直观,但更好的是 c (x1,x2,x3,...),c(y1,y2,y3,...),但这仅与翻转问题有关。
输入示例

The user gives pairs of ids which corresponds to ids in the upper triangle that is when Y > X.I am thinking how you should handle the points and which data structure is best here.I think having pairs like (x1,y1), (y3,x3), ... is intuitive but better can be c(x1,x2,x3,...), c(y1,y2,y3, ...) but then it is only about the flipping problem.Example inputs


  1. (10,1),(7,3),(5,4)-返回相同的

  2. (1,10),(7,3),(5,4)-返回(10,1),(7,3),(5,4)

  3. (1,10),(7,3),(4,5)-返回(10,1),(7,3),(5,4)

伪代码,我仍然不确定 id.pairs 或应该

Pseudocode where I am still not sure about the data structure in id.pairs or shoul

  lapply(id.pairs,function(z){
    x <- z$V1
    y <- z$V2
    ...
  })

 # not sure if possible
 lapply({x.points, y.points}, function(z){
    x <- z$V1
    y <- z$V2
    ...
  })

动机:为上三角中的以下功能给出正确的点

Motivation: to give correct points for the following function in the upper triangle

# https://stackoverflow.com/q/40538304/54964
cb(plt, x=c(10, 7, 5), y=c(1, 3, 4), rectArgs=list(border="red", lwd=3))



测试MarkPeterson的



我认为他的第一种方法可以工作,但是我在考虑如何应用他的 lapply 到参数 xleft ybottom

# Complete test code http://paste.ubuntu.com/23461804/
# Chat of https://stackoverflow.com/q/40538304/54964 user20650
cb <- function(corrPlot, ..., rectArgs = list() ){
                lst <- list(...)
                lapply({x,y}, function(x){
                  c(max(x), min(x))
                })

                xleft <- match(lst$x, colnames(corrPlot)) - 0.5
                ybottom <- n - match(lst$y, colnames(corrPlot)) + 0.5
}

R:3.3.1

操作系统:Debian 8.5

R: 3.3.1
OS: Debian 8.5

推荐答案

如果我们假设配对点列表,并且您可以相信这些值是有效的(可能不是一个合理的假设,但是可以添加防御措施),看来您可能只需使用 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.

这篇关于如何在R中将对翻转为X> Y顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:04