当遍历向量时,是否可以将元素的索引与元素一起使用?
a.vector <-c(“a”,“b”,“c”,“a”,“d”)
假设我需要a.vector的“第一个”“a”的索引。
一个不能使用
which(a.vector == "a")
因为有两个'a',它将返回两个位置1和4。我需要循环立即覆盖的元素的特定索引。
我需要这样的东西:
b.vector <-c(“the”,“cat”,“chased”,“a”,“mouse”)
for (i in a.vector) {
element<-b.vector[INDEX.OF(a.vector)])
-------some process using both 'element' and "a"-------}
这似乎类似于python中的“枚举”功能。解决方案将有很大帮助。谢谢。
最佳答案
只循环索引号怎么样?
for (i in seq_along(a.vector)){
a.element <- a.vector[i]
b.element <- b.vector[i]
...
}
关于r - R:遍历向量时使用元素的位置信息。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262735/