简单的 For 循环问题
Simple For loop Issues
我有多个向量要比较重叠。我在 for 循环中执行此操作,但 for 循环未正确加载向量。请在下面找到一个可重现的例子
#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)
}
给出一个字符(0)列表
也许还有更好的方法来做到这一点,但要使您的示例有效:
#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)
}
我有多个向量要比较重叠。我在 for 循环中执行此操作,但 for 循环未正确加载向量。请在下面找到一个可重现的例子
#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)
}
给出一个字符(0)列表
也许还有更好的方法来做到这一点,但要使您的示例有效:
#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)
}