【问题标题】:Deleting items off a list as I go in R loop在 R 循环中删除列表中的项目
【发布时间】:2021-05-18 19:49:56
【问题描述】:

提前为我的低效代码道歉,还在学习中!我正在尝试创建一个循环,该循环(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"
)

【问题讨论】:

  • 请以可以复制和粘贴的格式提供示例数据(例如dput()的输出)。
  • 现在使用 dput() 添加,这样行吗?
  • 是的,它有效。如果您还可以提供您所追求的输出示例,将更容易理解您的问题。
  • 您是否有特殊原因需要在执行过程中删除列表中的项目?这样做会导致一些奇怪的事情发生(如您所见)。与其随手删除,不如创建一个辅助数组,在其中只添加要保留的项目?
  • 谢谢大家!我的最终目标是将其余部分排序并存储在一个不同的矩阵中,以便在后续分析中使用。我想从这部分代码中缩短每个 split[[n]] 列表中的项目以仅保留相关项目。创建辅助数组的目的与我想的相同,但我试图简化流程,因为目前我有 n=148 并且可能正在处理更多。

标签: r loops for-loop nested-lists


【解决方案1】:

我不能 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

【讨论】:

  • 感谢完美,感谢您的提示!
  • 没问题,如果对您有用,请将其标记为已解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多