# Simple For Loop Issues

## 17 March 2022 - 1 answer

I have multiple vectors that I want to compare for overlap. I'm doing this in a for loop but the for loop is not loading the vectors properly. Please find below a reproducable example

``````#creating 4 vectors
TVector1 <- c("test1", "test2")
TVector2 <- c("test1", "test2")

XVector1 <- c("test1", "test2")
XVector2 <- c("test1", "test2")

# creating the list with the for loop
List_intersect <- list()
for (i in (1:2){ # length calculates the length of the vector# iterate from 0 to 19
identity1 = paste0("TVector", i)
identity2 = paste0("Xvector",i)
Intersect <- intersect(identity1, identity2)
List_intersect[[i]] <- (Intersect)
}
``````

It gives a character(0) list

Maybe there are also better ways to do this, but to make your example work:

``````#creating 4 vectors
TVector1 <- c("test1", "test2")
TVector2 <- c("test1", "test2")

XVector1 <- c("test1", "test2")
XVector2 <- c("test1", "test2")

# creating the list with the for loop
List_intersect <- list()
for (i in 1:2){
identity1 = eval(parse(text=paste0("TVector", i)))
identity2 = eval(parse(text=paste0("XVector", i)))
List_intersect[[i]] <- intersect(identity1, identity2)
}
``````