double for loop with an if condition

I am wondering what is the problem here?
I need the output as follows:

"a*" "b" "c*" "d" "e*"

data1 <- c("a", "b", "c", "d", "e")
data2 <- c("a", "c", "e")
for (i in data1){
for (j in data2){
if_else(i == j, paste0(data1[i], "*"),
data1[i])
}

}

data1

data1
[1] "a" "b" "c" "d" "e"

Hello,

you have the following problem: Your index i and j are along your vector entries, not the positions. This will work without an error:

library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> Die folgenden Objekte sind maskiert von 'package:stats':
#> 
#>     filter, lag
#> Die folgenden Objekte sind maskiert von 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data1 <- letters[1:5]
data2 <- c("a", "c", "e")
for (i in seq_along(data1)){
  for (j in seq_along(data2)){
    if_else(data1[[i]] == data2[[j]],
            paste0(data1[[i]], "*"),
            data1[[i]])
  }
}

Created on 2022-08-20 by the reprex package (v2.0.1)

If you wish to keep the result, you have to define a variable which allows replacement (e.g. replace inside data1 with data1[[i]] <- or define a new variable).

library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> Die folgenden Objekte sind maskiert von 'package:stats':
#> 
#>     filter, lag
#> Die folgenden Objekte sind maskiert von 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data1 <- letters[1:5]
data2 <- c("a", "c", "e")
for (i in seq_along(data1)){
  for (j in seq_along(data2)){
    data1[[i]] <- if_else(data1[[i]] == data2[[j]],
            paste0(data1[[i]], "*"),
            data1[[i]])
  }
}

data1
#> [1] "a*" "b"  "c*" "d"  "e*"

Created on 2022-08-20 by the reprex package (v2.0.1)

Kind regards

1 Like

Hello @FactOREO,

Thank you for your reply. It works perfectly. I just wonder, as you said, if I want to capture the new generated data in a vector other than data1, how could it work then ?

I tried to add outside the for loop an empty vector (data3 <- c()) then replace inside the for loop:
data1[[i]] withdata3[[i]], but it didn't work.

Thank you in advance.

Yes, this cannot work indeed. But since you know the dimension of the new vector (here 5), you can create an empty vector with length 5 beforehand. This way you won't get any 'subscript out of bounds' error. Just add my_results_vector <- vector(length=5) in front of the loop and then asign the value to my_results_vector[[i]].

1 Like

Super. Thank you again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.