0

I'm trying to count the number of time each row in my dataframe have both "Passed" in the result column and "Yes" in position. However, I'm getting an error in a loop I'm running.

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

I heard that this can happen when you compare to null or NA values but the values in the table are never null or NA.

This is the code I have for my loop.

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

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Anonymous
  • 17
  • 1
  • 7
  • 2
    If you search a little more through stackoverflow, you will find this exact error numerous times. If you manually run your loop (I'd check both `i <- 1` and `i <- length(recent)` for starters), and then check the "if" condition manually, you will find that it does not always return exactly one `logical` response. – r2evans Nov 06 '18 at 01:16

1 Answers1

0

You don't really need a loop to do that. Why don't you just write the following:

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)

Hope that helps.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44