我有多個要比較重疊的向量。我在 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)串列
uj5u.com熱心網友回復:
也許還有更好的方法可以做到這一點,但要使您的示例有效:
#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)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446344.html
上一篇:創建一個資料框,該資料框是來自其他資料框R的向量之間的差異的結果
下一篇:“新”運算子中的for回圈
