Charlie
Charlie

Reputation: 707

R - Multiple variables in for loop?

How can I create a for loop in R which considers 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 would generate 10.

EDIT: Thanks for yor 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 represent these outputs? Thanks again.

Upvotes: 12

Views: 67387

Answers (6)

Pavel
Pavel

Reputation: 1

I found a rather easy solution if you would like to still stick with your For Loop and have multiple variables.

You simply need to use the loop to call the variables based on the index rather than directly calling the variable itself. The vectors will need to be of the same length.

so the loop will look something like this:

for (h in 1:length(Vector1)) {

i <- Vector2[h]

j <- Vector3[h]

k <- Vector4[h] ....etc
}

Hope that helps!

Upvotes: 0

Erim Kaan Bayraktar
Erim Kaan Bayraktar

Reputation: 1

I realized that the solutions above work when i and j are equal. For the situations where i and j are not equal, I use the code below:

a <- 5 
for(i in 4:6){
  for(j in a:7){
    print(i*j)
    a <- a+1
    break
  }
}

The result is 20,30,42

Upvotes: 0

RHertel
RHertel

Reputation: 23788

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 a different possibility to obtain this result more quickly:

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 is here that you have just one small loop that runs only over those entries which are equal in both vectors (six numbers in this case), instead of the nested loops which run over the entire 100 entries of the matrix.

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

print(c(m))

As far as the changes connected with your edit are concerned, I assume that 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))

Upvotes: 1

James
James

Reputation: 66834

You can use outer to perform 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

Upvotes: 2

Benjamin
Benjamin

Reputation: 17359

It seems like you're asking about a nested for 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)

Upvotes: 8

josliber
josliber

Reputation: 44299

You could do this with nested for loops:

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

Upvotes: 1

Related Questions