【问题标题】:Need help coding an answer in R需要帮助在 R 中编写答案
【发布时间】:2018-10-17 20:18:00
【问题描述】:

我不知道在这个问题中 X 或 Y 向量是如何相互关联的。答案是单个向量这一事实最让我困惑。

我尝试做负排名(即排名(-s)),然后尝试

rev(rank(X)) 和 rev(rank(Y)) 我得到不正确的排名返回。这是我的代码:

x <- c(-1, 0, 1, 2, -3) #vector x
y <- c(1, 3, 2, 5, 8) # Vector y 
S <- x[order(match(x,y))] #orders and matches x with y 
R <- rank(-S) #ranks S in element number order 
print(R) #I want R = (5 2 1 3 4)

相反,我得到[2 1 4 3 5]

使用其他代码:

never <- rev(rank(-x)) #takes the reverse rank of x 
bird <- rev(rank(y)) #takes the reverse rank of y
kid <- x[order(match(never, bird))] #orders the match of the reverse ranks
tr <- rank(kid) #ranks the match of the reverse ranks 
print(tr) 

我收到[2 1 3 4 5]。

【问题讨论】:

  • Pr0P5 4 dE 0utr1Ght bRAzenNes5
  • 好吧,我们知道你擅长一件事。

标签: r


【解决方案1】:

据我了解这个问题(不清楚,我同意),似乎有一个错误,但是 $x$ 和 $y$ 由 $s_i$ 索引链接。

根据问题:让我们找到 $s_1$ 索引,这要感谢 $y$。我们知道 $y_{s_1}$ 是 $y$ 的最大值,因此 $y_5$ 在这种情况下。因此,$s_1 = 5$,所以我们需要找到$x_5 = -3$。由于这是最低值,我们有 $r_1 = 5$。

现在使用这种方法,我们将有 $s_2 = 4$,所以我的猜测是示例或问题中存在错误。

在示例中,我碰巧有 $R = (5, 1, 3, 2, 4)$。

您可以注意到,如果您对 $y$ 进行排序,则将排序规则应用于 $x$ 创建$x'$,$R$向量是$x'$.

【讨论】:

    【解决方案2】:

    我想通了。

    x <- c(-1,0,1,2,-3)
    y <- c(1,3,2,5,8)
    
    myrank <- function(x,y)
    {
      R=c()
         {
           for(i in 1:length(x))
              {
                 b=length(y)+1-rank(y)
                 a=length(y)+1-rank(x[b]) # here x[b] is to order the x values according to b
                 R=c(a)
              }
         }
      print(R)
    }
    myrank(x,y)
    

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多