[R] if else for cumulative sum error

Jefferson Ferreira-Ferreira jecogeo at gmail.com
Tue Dec 2 19:08:07 CET 2014


Hello everybody;

I'm writing a code where part of it is as follows:

for (i in nrow(dadosmax)){
  dadosmax$enchday[i] <- if (sum(dadosmax$above[i:(i+44)]) >= 45) 1 else 0
}

That is for each row of my data frame, sum an specific column (0 or 1) of
that row plus 44 rows. If It is >=45 than enchday is 1 else 0.

The following error is returned:

Error in if (sum(dadosmax$above[i:(i + 44)]) >= 45) 1 else 0 :
  missing value where TRUE/FALSE needed

I've tested the ifelse statement assigning different values to i and it
works. So I'm wondering if this error is due the fact that at the final of
my data frame there aren't 45 rows to sum anymore. I tried to use "try" but
It's simply hide the error.

How can I deal with this? Any ideas?
Thank you very much.

	[[alternative HTML version deleted]]



More information about the R-help mailing list