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]])
}
}
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*"
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.
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]].