当我进入 R 循环时从列表中删除项目

Deleting items off a list as I go in R loop

为我低效的代码提前道歉,还在学习中!我正在尝试创建一个循环,该循环 (1) 从语义字符列表中获取项目,将相关的项目复制到新矩阵中,以及 (2) 删除我复制的项目。我完成了第 1 部分,但无法让第 2 部分工作。它仅适用于前两行,然后我收到“[[n]] 中的错误:下标越界”。

这是实现第 1 部分的代码:

for(i in 1:length(split)) {
  a <- strsplit(split[[i]], "\.") #split each semantic version numeral into individual numbers
  x <- length(a)
  for (n in 1:x){  #for each element in vector r, check if a comment or character
    if (a[[n]][1]==0) {       #comments have a 0 as the first value
        a <- sapply(a, paste, collapse = ".")
        atmatrix[i,n] <- a[n]
        a <- strsplit(a, "\.")
    }
  }
  
}

我尝试包括第 2 部分

   for(i in 1:length(split)) {
  a <- strsplit(split[[i]], "\.") #split each semantic version numeral into individual numbers
  x <- length(a)
  for (n in 1:x){  #for each element in vector r, check if a comment or character
    if (a[[n]][1]==0) {       #comments have a 0 as the first value
        a <- sapply(a, paste, collapse = ".")
        atmatrix[i,n] <- a[n]
        a <- a[-n]
        a <- strsplit(a, "\.")
        x <- length(a)
    }
split[[i]] <- a
  }
  
}

非常感谢任何有关如何在同一循环中完成第 2 部分的帮助!以下 split[[i]] 项目之一的示例:

c("0.3 insect damage", "0.12 Apex preserved", "0.113 tertiary", 
"0.14 >1/2 margin preserved", "0.21 USNM type", "1.0 not observed", 
"2.0 not observed", "3.0 not observed", "4.0 not observed", "5.0 not observed", 
"6.0 not observed", "7.0 not observed", "8.4 laminar size notophyll", 
"9.0 not observed", "10.1 laminar shape elliptic", "11.1 medial symmetry", 
"12.0 not observed", "13.1 unlobed", "14.1 untoothed (margin entire)", 
"15.0 not observed", "16.2 obtuse apex angle", "17.2 convex apex", 
"18.0 not observed", "19.0 not observed", "20.0 not observed", 
"21.0 not observed", "22.0 not observed", "23.1 primary venation pinnate", 
"24.0 not observed", "25.0 not observed", "26.0 not observed", 
"27.3.1 major secondaries simple brachidodromous", "28.1 interior secondaries absent", 
"29.0 not observed", "30.0 not observed", "31.2 irregular spacing", 
"32.1 uniform angle", "33.3 excurrent attachment to midvein", 
"34.1.1 proximal course is parallel to major secondaries", "34.2.2 intersecondary length >50% of subjacent secondary", 
"34.3.3 distal course perpendicular to a subjacent major secondary", 
"34.4.2 vein frequency usually 1 per intercostal area", "35.1.1.1.1 intercostal tertiary vein fabric opposite percurrent with straight course", 
"35.1.2.2 obtuse to midvein", "36.4 exmedially decreasing vein angle", 
"37.1.1.1 epimedial tertiaries opposite percurrent", "37.2.1.1 proximal course parallel to the subjacent secondary", 
"37.2.2.1 distal course parallel to the intercostal tertiaries", 
"38.0 not observed", "39.0 not observed", "40.0 not observed", 
"41.0 not observed", "42.0 not observed", "43.0 not observed", 
"44.0 not observed", "45.0 not observed", "46.0 not observed", 
"47.0 not observed", "48.0 not observed", "49.0 not observed", 
"50.0 not observed", "51.0 not observed", "52.0 not observed"
)

我不是 100% 确定我明白你想做什么,但是,你收到越界错误的原因是

  1. 您在此处设置值 x
x <- length(a)
  1. 然后您在此处覆盖整个列表a
a <- sapply(a, paste, collapse = ".")

新创建的 a 与原始 a 的长度不同(即,它不是 x),因此在尝试时抛出越界错误访问它。

如果您要替换 a 中的特定项目,请考虑分配给 a[[n]]

相反,要简单地删除列表中的任意成员,您可以尝试:

listname[[indexnumber]] <- NULL

但是 小心删除您正在循环访问的列表中的项目:这会在您进行时扰乱列表的索引。最好存储一个包含要删除的列表项索引的向量,然后在最后:

# Where del_vec is the vector containing the index positions to delete, e.g.,
del_vec <- c(1, 4, 6, 8)

# Delete all at once
listname[del_vec] <- NULL # note the single square bracket