对于循环输出不如预期

时间:2017-06-21 12:05:21

标签: r for-loop if-statement

我有以下嵌套for循环的条件,但似乎,条件不被认为。

i = 1
for(fa in c(1, 2, 3, 4, 5, 6, 8, 9, 11)){
  for(wz in c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "O", "P", "Q", "R", "S", "U")){
    avg_fzg <- fzg_aktiv_wv[fzg_aktiv_wv$K01 == fa & fzg_aktiv_wv$K19b == wz, c("K00", "K01", "K31c", "K34c")]
    avg_fahrten <- fzg_fahrten_merge
    avg_fahrten <- avg_fahrten[avg_fahrten$K01 == fa & avg_fahrten$K19b == wz & avg_fahrten$F15 != -1, c("K00", "K01", "K19b", "F14", "F15", "F04a")]
    avg_fahrten <- avg_fahrten[!duplicated(avg_fahrten),]

    if(length(avg_fzg$K01 >= 10)){
      print(length(avg_fzg$K01))
      Fzg_WZ_avg[i,1] <- median(avg_fzg$K31c)
      Fzg_WZ_avg[i,2] <- median(avg_fzg$K34c)
      Fzg_WZ_avg[i,3] <- median(avg_fahrten$F14)
      Fzg_WZ_avg[i,4] <- median(avg_fahrten$F15)
      Fzg_WZ_avg[i,5] <- median(avg_fahrten$F04a)


    }

  i <- i + 1
  } 
}

print语句供我检查,length(avg$K01)正在按我想要的方式工作 - 而且确实如此,但条件中的所有内容都已执行。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

你的if语句陈述

length(avg_fzg$K01 >= 10)

我想,应该是

length(avg_fzg$K01) >= 10

请进行更正并编译。