使用不同的变量创建for循环

时间:2017-06-27 19:24:32

标签: r for-loop

对于那些习惯于制作"对于那些人而言,这可能是一个非常简单的问题。循环。以下是我尝试使用循环(AA是矩阵):

lines(x = c(AAx[1:2,1]), y = c(AAx[1:2,2]))
lines(x = c(AAx[3:4,1]), y = c(AAx[3:4,2]))
lines(x = c(AAx[5:6,1]), y = c(AAx[5:6,2]))
lines(x = c(AAx[7:8,1]), y = c(AAx[7:8,2]))
lines(x = c(AAx[9:10,1]), y = c(AAx[9:10,2]))
lines(x = c(AAx[11:12,1]), y = c(AAx[11:12,2]))
lines(x = c(AAx[13:14,1]), y = c(AAx[13:14,2]))
lines(x = c(AAx[15:16,1]), y = c(AAx[15:16,2]))
lines(x = c(AAx[17:18,1]), y = c(AAx[17:18,2]))
lines(x = c(AAx[19:20,1]), y = c(AAx[19:20,2]))
lines(x = c(AAx[21:22,1]), y = c(AAx[21:22,2]))
lines(x = c(AAx[23:24,1]), y = c(AAx[23:24,2]))

我试过了:

i <- c(1,3,5,7,9,11,13,15,17,19,21,23)
j <- c(2,4,6,8,10,12,14,16,18,20,22,24)

for (i in i){
  for (j in j){
    lines(x = c(AAx[i:j,1]), y = c(AAx[i:j,2]))
  }
}

但没有成功。我确信有一种简单的方法,但现在还无法解决。任何帮助表示赞赏。

我有第二个但类似的问题:

text(AAx[1,1],AAx[1,2], names[1], pos = 3)
text(AAx[3,1],AAx[3,2], names[2], pos = 3)
text(AAx[5,1],AAx[5,2], names[3], pos = 3)
text(AAx[7,1],AAx[7,2], names[4], pos = 3)
text(AAx[9,1],AAx[9,2], names[5], pos = 3)
text(AAx[11,1],AAx[11,2], names[6], pos = 3)
text(AAx[13,1],AAx[13,2], names[7], pos = 3)
text(AAx[15,1],AAx[15,2], names[8], pos = 3)
text(AAx[17,1],AAx[17,2], names[9], pos = 3)
text(AAx[19,1],AAx[19,2], names[10], pos = 3)
text(AAx[21,1],AAx[21,2], names[11], pos = 3)
text(AAx[23,1],AAx[23,2], names[12], pos = 3)

names <-  c(“AA”,”BB”,”CC”,”DD”,”EE”,”FF”,”GG”,”HH”,”EE”,”JJ”,”KK”,”LL”)
j <- c(1,3,5,7,9,11,13,15,17,19,21,23)

我试过了:

for (i in j){
  for (k in 1:length(names)){
    text(AAx[i,1],AAx[i,2], names[k], pos = 3)
    }
  }

我从第一个问题收到了有用的答案,但我仍然坚持第二个问题。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

搞定了

Application Insights
相关问题