首页 > 解决方案 > 遍历存储在向量中的值

问题描述

在下面有代码,我试图找出唯一的区域名称然后迭代,但是当我运行它时它只打印出 1 行。

这是我的代码:

 ZonelistName<-unique(temp[c("zone")])
  
  #this is where the LSD for the zone will be
  
  for (p in c(ZonelistName)) {
    print(p)
  }

打印出来:

# > [1] 3 2 5 1



   dput(head(ZonelistName))
structure(list(zone = c(3, 2, 5, 1)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

抱歉,我无法复制它,因为我不知道那个对象是什么

标签: rloopsvector

解决方案


我想你可能需要unlist而不是c

for (p in unlist(ZonelistName)) {
  print(p)
}

或者

for (p in ZonelistName$zone) {
  print(p)
}

这使

[1] 3
[1] 2
[1] 5
[1] 1

推荐阅读