R - Multiple variables for a loop?

How do I create a loop for

in R that looks at two variables?

Something like:

for(i in 1:10, j in 1:10) {
if vector[j] == vector2[i]
print(variable)
else print(NA) }

      

This should give me 100 outputs as opposed to using

vector[i] == vector[i]

      

which will generate 10.

EDIT: Thanks for the help so far. here is my actual data:

for(i in 1:10) {
    for(j in 1:10) {
        if (i == j)
        print(NA)
        else if(st231_eq1_alg$Output[j] == st231_eq1_alg$Input[i])
        print(st231_eq1_alg_f[i])
        else if(st231_eq1_alg$Output[j] == st231_eq1_alg$Output[i])
        print(st231_eq1_alg_inv_f[i])
        else print(NA)
    }
}

      

Any ideas how best to present these results? Thanks again.

+3


source to share


4 answers


You seem to be asking about a nested loop

for (i in 1:10){
  for(j in 1:10){
    ...
  }
}

      



But I would recommend a different approach

Vectors <- expand.grid(vector1 = vector1,
                       vector2 = vector2)
Vectors$comparison <- with(Vectors, vector1 == vector2)

      

+3


source


You can use outer

to accomplish this and get a 10x10 matrix with the results:



vector <- 1:10
vector2 <- sample(vector)
outer(vector,vector2,"==")
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 [2,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [3,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 [4,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
 [5,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [6,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [7,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
 [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
 [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

      

+2


source


You can do this with nested loops:

for (i in 1:10) {
  for (j in 1:10) {
    # Your logic in here
  }
}

      

+1


source


Your code works if you write two nested loops:

for(i in 1:10} {
  for (j in 1:10) {
   if vector[j] == vector2[i]
    print(variable)
  else 
    print(NA)
 }
}

      

The following example shows another way to get this result faster:

set.seed(1234)
vector <- sample(20,10)
vector2 <- sample(20,10)
same <- vector[vector %in% vector2]
m <- matrix(NA,ncol=10,nrow=10)
for (i in 1:length(same)) m[vector==same[i], vector2==same[i]] <- same[i]
#> vector
# [1]  3 12 11 18 14 10  1  4  8  6
#> vector2
# [1] 14 11  6 16  5 13 17  4  3 12
#> m
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]   NA   NA   NA   NA   NA   NA   NA   NA    3    NA
# [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    12
# [3,]   NA   11   NA   NA   NA   NA   NA   NA   NA    NA
# [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
# [5,]   14   NA   NA   NA   NA   NA   NA   NA   NA    NA
# [6,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
# [7,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
# [8,]   NA   NA   NA   NA   NA   NA   NA    4   NA    NA
# [9,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
#[10,]   NA   NA    6   NA   NA   NA   NA   NA   NA    NA 
#> same
#[1]  3 12 11 14  4  6

      

The advantage here is that you only have one small loop that only works on items that are equal in both vectors (six numbers in this case), instead of nested loops that run through all 100 entries of the matrix.

If you want to consistently print the entries of this matrix, as in your nested loops, you can type:

print(c(m))

      

As for the changes related to your edit, I guess this should work:

same1 <- st231_eq1_alg$Output[st231_eq1_alg$Output %in% st231_eq1_alg$Input]
idx2 <- which(duplicated(st231_eq1_alg$Output))
same2 <- st231_eq1_alg$Output[idx2]
m <- matrix(NA, ncol = 10, nrow = 10)
for(i in 1:length(same1)) m[st231_eq1_alg$Output==same1[i], st231_eq1_alg$Input==same1[i]] <- same1[i] 
for(i in 1:length(same2)) m[st231_eq1_alg$Output==same2[i], st231_eq1_alg$Output==same2[i]] <- st231_eq1_alg_inv_f[idx2[i]]
print(c(m))

      

+1


source







All Articles