【发布时间】: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