简体   繁体   English

在 R 中嵌套循环以在满足条件时给出 DF 位置

[英]Nested loop through in R to give DF position when criteria is met

I am running a nested for loop with an If-clause to determine which correlation in my matrix is above 90% (.9) or less than 100% (1).我正在运行带有 If 子句的嵌套 for 循环,以确定矩阵中哪个相关性高于 90% (.9) 或低于 100% (1)。 When running the loop I created it only gives the below output:运行我创建的循环时,它只提供以下输出:

问题

Please see my code below:请看我下面的代码:

colss = ncol(x)
rowss = nrow(x)

for (i in rowss){
 for (j in colss){
   if (x[i,j] > .9 || x[i,j] < 1){
    print(x[i,j])
  }
 }
}   

Is there a way to get the position in the DF that has the values needed (>.9 || < 1)?有没有办法在 DF 中获得具有所需值的位置(>.9 || < 1)?

Example output:示例输出:

[12] .90 [12] .90

[15] .92098 [15] .92098

Disclosure: I tried looking at others questions that were similar to mine but the answers didn't make total sense, so if you could please dumb it down I would appreciate it.披露:我尝试查看与我类似的其他问题,但答案并不完全有意义,因此,如果您能将其简单化,我将不胜感激。

To get position that match your criteria, try which要获得符合您标准的职位,请尝试使用which

DF <- matrix(runif(20),4)
DF
#           [,1]      [,2]      [,3]      [,4]      [,5]
# [1,] 0.0794861 0.5229619 0.1095744 0.3804106 0.2984173
# [2,] 0.9484121 0.5484754 0.6794459 0.6099729 0.3454522
# [3,] 0.2310753 0.8656815 0.4991712 0.8683982 0.8322723
# [4,] 0.9121636 0.1028238 0.4266272 0.3025331 0.3265509

which(DF > 0.9 & DF < 1)
# [1] 2 4

which(DF > 0.9 | DF < 1)
#  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM