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]]
On Dec 2, 2014, at 10:08 AM, Jefferson Ferreira-Ferreira wrote:> 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?You should think about what would happen at the high end of values (for what is now being erroneously coded) for `nrow(dadosmax)` which a assume was meant to be `1:nrow(dadosmax)`. Should have been `seq_along(rownames(dadosmax))`. But even if that error were fixed you would be referencing non-existent rows as soon as you were within 44 rows of the end of the dataframe. -- David.> Thank you very much. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA
On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira < jecogeo at gmail.com> wrote:> 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 > } >?Without some test data for any validation, I would try the following formula dadosmax$enchday[i] <- if (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))]>= 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- The temperature of the aqueous content of an unremittingly ogled culinary vessel will not achieve 100 degrees on the Celsius scale. Maranatha! <>< John McKown [[alternative HTML version deleted]]
Thank you for replies.
David,
I tried your modified form
for (i in 1:seq_along(rownames(dadosmax))){
dadosmax$enchday[i] <- if ( (sum(dadosmax$above[i:(i+44)])) >= 45) 1
else
0
}
However, I'm receiving this warning:
Warning message:
In 1:seq_along(rownames(dadosmax)) :
numerical expression has 2720 elements: only the first used
I can't figure out why only the first row was calculated...
Any ideas?
Em Tue Dec 02 2014 at 15:22:25, John McKown <john.archie.mckown at
gmail.com>
escreveu:
> On Tue, Dec 2, 2014 at 12:08 PM, Jefferson Ferreira-Ferreira <
> jecogeo at gmail.com> wrote:
>
>> 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
>> }
>>
>
> ?Without some test data for any validation, I would try the following
> formula
>
> dadosmax$enchday[i] <- if
> (sum(dadosmax$above[i:(min(i+44,nrow(dadosmax)))] >= 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]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> The temperature of the aqueous content of an unremittingly ogled
> culinary vessel will not achieve 100 degrees on the Celsius scale.
>
> Maranatha! <><
> John McKown
>
[[alternative HTML version deleted]]