I have a 2 variables with various lengths. I want to create a for loop that only calculate the sum for two equal values. If the values aren’t equal to each other, the variable b must be updated with +1. Is there a way to create this? I thougth something like this:
a <- c(1,2,3,4,5)
b<- c(1,7,2,3,6,4,5)
j <- 1
test<- matrix()
for( i in 1:length(a)) {
if(a[i] == b[j]){
result <- a[i] + b[j]
test[[i]]<-matrix(result)
j <- j + 1}
else {
j <- j +1
}
1 + 1 = TRUE
2 + 7 + FALSE +1
2 + 2 = TRUE
3 + 3 = TRUE
4 + 6 = FALSE +1
4 + 4 = TRUE
5 + 5 = TRUE
Thank you all!
I think that this is the right solution for this variables. The only requirement is that all values from variable a need to provent in variable b.