Dan Stanger
2010-Jun-18 15:39 UTC
[R] Summing using a boolean index vector (repost in plain text).
Hello all: I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays<-(diff(f$weekdays) < -1). I would like to create a vector of sums, for each week. That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. Is there a vector operation which can do this, without writing an explicit loop? Thank you, Dan Stanger P.S. Sorry about the repost. Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261
jim holtman
2010-Jun-18 16:23 UTC
[R] Summing using a boolean index vector (repost in plain text).
?cumsum ?ave But without data (follow the posting guide) specific solution can not be specified On Fri, Jun 18, 2010 at 11:39 AM, Dan Stanger <DStanger at eatonvance.com> wrote:> Hello all: > I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays<-(diff(f$weekdays) < -1). > I would like to create a vector of sums, for each week. ?That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. > Is there a vector operation which can do this, without writing an explicit loop? > Thank you, > Dan Stanger > P.S. Sorry about the repost. > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Dan Stanger
2010-Jun-18 17:12 UTC
[R] Summing using a boolean index vector (repost in plain text).
Hello Jim, Thank you for getting back to me. Cumsum does exactly what I needed as the following example shows. [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE> cumsum(x)[1] 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: Friday, June 18, 2010 12:24 PM To: Dan Stanger Cc: R-help at r-project.org Subject: Re: [R] Summing using a boolean index vector (repost in plain text). ?cumsum ?ave But without data (follow the posting guide) specific solution can not be specified On Fri, Jun 18, 2010 at 11:39 AM, Dan Stanger <DStanger at eatonvance.com> wrote:> Hello all: > I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays<-(diff(f$weekdays) < -1). > I would like to create a vector of sums, for each week. ?That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. > Is there a vector operation which can do this, without writing an explicit loop? > Thank you, > Dan Stanger > P.S. Sorry about the repost. > Eaton Vance Management > 200 State Street > Boston, MA 02109 > 617 598 8261 > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?