如何在 r 中一次循环遍历两个变量

How to loop through two variables at once in r

我正在尝试在 R 中一起循环遍历 2 个变量。

这是我的

for (x in c(gl_50, gl_100, gl_200,gl_300,gl_400,gl_500,gl_600, gl16)) {
     for (y in c(50, 100, 200, 300, 400, 500, 600, 670)) {
  gi_x <- gl2gi(x)
  sum_x <- basic.stats(gi_x)
  
  ind<-colMeans(sum_x$n.ind.samp, na.rm=T)
  Ho<-colMeans(sum_x$Ho, na.rm = T)
  Hs<-colMeans(sum_x$Hs, na.rm = T)
  Fis<-colMeans(sum_x$Fis, na.rm = T)

  sum<-data.frame(cbind(ind, Ho, Hs, Fis))
  sum$snp_no <- rep(y)
  print(sum)

当前结果是循环运行 gl_50 和所有 y 值,然后 gl_100 和所有 y 值,依此类推。我想要的是 gl_50 50,gl_100 100,gl_200 200 等等。

通常,您应该遍历它们的索引并像这样访问它们:

a <- c(gl_50, gl_100, gl_200,gl_300,gl_400,gl_500,gl_600, gl16)
b <- c(50, 100, 200, 300, 400, 500, 600, 670)
for (i in seq_along(a)) {
  a[i] 
  b[i]  
}