为什么会出现错误,循环中的参数长度为0

时间:2018-11-06 01:07:41

标签: r

我正在尝试计算数据帧中每一行在结果列中均“通过”并且在位置上“是”的时间。但是,我在运行的循环中遇到错误。

Error in if (current$position == "Yes" & current$result == "Passed") { : 
  argument is of length zero

我听说当您与nullNA值进行比较时会发生这种情况,但是表中的值永远不会是nullNA

这是我的循环代码。

count_votes <- 0
recent <- slice(flatten_votes, 1:20)

for (i in 1:length(recent)) {
  current <- slice(recent, i)
  if (current$position == "Yes" & current$result == "Passed") {
    count_votes = count_votes + 1
  }
}

This is what the first few rows of the dataframe look like

1 个答案:

答案 0 :(得分:0)

您实际上并不需要循环即可。您为什么不写以下内容:

my.df = flatten_votes #or recent
passed_and_yes <- my.df[my.df$position == "Yes" & my.df$result == "Passed",]
count_votes <- nrow(passed_and_yes)

希望有帮助。