r - R:遍历向量时使用元素的位置信息。

标签 r loops vector indexing

当遍历向量时,是否可以将元素的索引与元素一起使用?

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/

相关文章:

r - R 中的元素和多个向量

r - 如何在 roxygen 识字编程中转义 %?

python - 在 R 中运行 azuremlsdk::install_azureml() 时出错

php - 使PHP循环以相反的顺序回显

c - 难倒 : for loop to build an array not working, 忽略初始条件?

c++ - 在一个范围内推送五个对象指针,然后检查对象的 bool 是否为 false,会出错

r - 使用 R 中 {boot} 的 boot() 函数在最高级别的集群数据上进行非参数引导

在 R 中运行 Maxent

actionscript-3 - 无法跳出嵌套的 for 循环

c++ - 指针 vector 而不是对象