Imagine I have this bit of nested for loop, which prints all combinations of a and b
a = c(1,2,3,4)
b = c(2,3,4,5)
for(i in a){
for(k in b){
print(i + k)
}}
So the output looks like this
[1] 3
[1] 4
[1] 5
[1] 6
[1] 4
[1] 5
[1] 6
[1] 7
[1] 5
[1] 6
[1] 7
[1] 8
[1] 6
[1] 7
[1] 8
[1] 9
How do I loop through the two loops to get a result with only 4 items, the sum of elements from a and b with the same index, akin to looping through a dictionary in Python? I would like to have result like this:
[1] 3
[1] 5
[1] 7
[1] 9
or this
[1] 3 5 7 9
Whereby I simply add up a and b like adding up two columns in a dataframe to produce a third of the same length.
I appreciate any help.