[R] Sum of vector elements

2012-08-07 Thread number10
Hi, Is it possible to avoid using do and while loops to calculate the sum of
the elements of a vector until the appearance of the first positive element.



--
View this message in context: 
http://r.789695.n4.nabble.com/Sum-of-vector-elements-tp4639395.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Sum of vector elements

2012-08-07 Thread R. Michael Weylandt
I'd do something like this:

x - sample(seq(-10, 10))

sum(x[seq_len(which.max(x  0)])

Though others might have more direct solutions.

which.max() gets you the index of the first time x  0 -- seq_len
gives you numbers 1 to that index -- then just subset and sum like
normal.

Best,
Michael

On Tue, Aug 7, 2012 at 7:37 AM, number10 havana_dr...@ymail.com wrote:
 Hi, Is it possible to avoid using do and while loops to calculate the sum of
 the elements of a vector until the appearance of the first positive element.



 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Sum-of-vector-elements-tp4639395.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.

__
R-help@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.


Re: [R] Sum of vector elements

2012-08-07 Thread Jeff Newmiller
Another approach... not exactly more direct, but perhaps more robust and more 
general:

tmp - aggregate(x,list(lvl=cumsum(abs(diff(c(FALSE,x0), FUN=sum)
ans - tmp[0==tmp$lvl,x]

abs(diff()) finds transitions, FALSE forces level zero to represent negative 
numbers
cumsum marks levels (groups of positive and not-positive numbers)
aggregate does the summation
ans may be empty if x started with positive numbers.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

R. Michael Weylandt michael.weyla...@gmail.com wrote:

I'd do something like this:

x - sample(seq(-10, 10))

sum(x[seq_len(which.max(x  0)])

Though others might have more direct solutions.

which.max() gets you the index of the first time x  0 -- seq_len
gives you numbers 1 to that index -- then just subset and sum like
normal.

Best,
Michael

On Tue, Aug 7, 2012 at 7:37 AM, number10 havana_dr...@ymail.com
wrote:
 Hi, Is it possible to avoid using do and while loops to calculate the
sum of
 the elements of a vector until the appearance of the first positive
element.



 --
 View this message in context:
http://r.789695.n4.nabble.com/Sum-of-vector-elements-tp4639395.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.

__
R-help@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.

__
R-help@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.


Re: [R] Sum of vector elements

2012-08-07 Thread William Dunlap
I'll add my suggestion, which first extracts from x the elements
up to but not including the first positive one and then sums them:
   f3 - function(x) { sum(x[cumsum(x0) == 0]) }
Michaels suggestion was
   sum(x[seq_len(which.max(x  0)])
but that includes the first positive element so I think it should be
at least
   f1 - function(x) sum(x[seq_len(which.max(x0)-1)])
(but that still fails in some cases).
Jeff's was
  f2 - function (x) {
  tmp - aggregate(x, list(lvl = cumsum(abs(diff(c(FALSE, x  0), FUN = 
sum)
  tmp[0 == tmp$lvl, x]
  }   

To test them, make a list of datasets
 xs - list(allNeg=c(-1, -3, -9), allPos=c(1,3,9), firstPos=c(1,-3,-9), 
 firstNeg=c(-1,-3,+9), misc=c(-1,-3,9,-27,-81))
 sapply(xs, function(x)try(f1(x)))
  allNeg   allPos firstPos firstNeg misc 
   000   -4   -4 
 sapply(xs, function(x)try(f2(x)))
$allNeg
[1] -13

$allPos
numeric(0)

$firstPos
numeric(0)

$firstNeg
[1] -4

$misc
[1] -4

 sapply(xs, function(x)try(f3(x)))
  allNeg   allPos firstPos firstNeg misc 
 -1300   -4   -4


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf
 Of Jeff Newmiller
 Sent: Tuesday, August 07, 2012 10:25 AM
 To: R. Michael Weylandt; number10
 Cc: r-help@r-project.org
 Subject: Re: [R] Sum of vector elements
 
 Another approach... not exactly more direct, but perhaps more robust and more 
 general:
 
 tmp - aggregate(x,list(lvl=cumsum(abs(diff(c(FALSE,x0), FUN=sum)
 ans - tmp[0==tmp$lvl,x]
 
 abs(diff()) finds transitions, FALSE forces level zero to represent negative 
 numbers
 cumsum marks levels (groups of positive and not-positive numbers)
 aggregate does the summation
 ans may be empty if x started with positive numbers.
 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
   Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
 /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.
 
 R. Michael Weylandt michael.weyla...@gmail.com wrote:
 
 I'd do something like this:
 
 x - sample(seq(-10, 10))
 
 sum(x[seq_len(which.max(x  0)])
 
 Though others might have more direct solutions.
 
 which.max() gets you the index of the first time x  0 -- seq_len
 gives you numbers 1 to that index -- then just subset and sum like
 normal.
 
 Best,
 Michael
 
 On Tue, Aug 7, 2012 at 7:37 AM, number10 havana_dr...@ymail.com
 wrote:
  Hi, Is it possible to avoid using do and while loops to calculate the
 sum of
  the elements of a vector until the appearance of the first positive
 element.
 
 
 
  --
  View this message in context:
 http://r.789695.n4.nabble.com/Sum-of-vector-elements-tp4639395.html
  Sent from the R help mailing list archive at Nabble.com.
 
  __
  R-help@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.
 
 __
 R-help@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.
 
 __
 R-help@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.

__
R-help@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.


Re: [R] sum in vector

2010-11-17 Thread Hadley Wickham
 rowsum(value, paste(factor1, factor2, factor3))

That is dangerous in general, and always inefficient. Imagine factor1
is c(a, a b) and factor2 is (b c, c).  Use interaction with
drop = T.

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-help@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.


[R] sum in vector

2010-11-14 Thread lgpeco

hy guys i have one question :)

i have two vectors markets and price

market - c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
price - c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4, 38, 96)
i would like sum prices: market 1: (100+4+96), market 2: (38),..., market 9:
(10+50)
ao i would like get this result: (200, 38, ..., 60)
and i don't wanna use while, for loops... is there any other function to sum
these :)

tnx for helping ;)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041661.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread Andrew Dolman
tapply(price, market, sum)

andydol...@gmail.com



On 14 November 2010 13:02, lgpeco pavic.o...@gmail.com wrote:

 hy guys i have one question :)

 i have two vectors markets and price

 market - c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
 price - c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4, 38, 96)
 i would like sum prices: market 1: (100+4+96), market 2: (38),..., market 9:
 (10+50)
 ao i would like get this result: (200, 38, ..., 60)
 and i don't wanna use while, for loops... is there any other function to sum
 these :)

 tnx for helping ;)
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041661.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.


__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread Alexx Hardt

Am 14.11.2010 13:02, schrieb lgpeco:

hy guys i have one question :)

i have two vectors markets and price

market- c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
price- c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4, 38, 96)
i would like sum prices: market 1: (100+4+96), market 2: (38),..., market 9:
(10+50)
ao i would like get this result: (200, 38, ..., 60)
and i don't wanna use while, for loops... is there any other function to sum
these :)

tnx for helping ;)
   


you could cbind() the vectors and use sum( result[row1==i] ) for market 
number i, I guess?

 -- Alexx

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread John Kane
Have a look at the reshape2 package.  http://had.co.nz/reshape/
reshape2 is, to the user a slight modification of rehape but the author says it 
is much faster.

The code below seems to do what you want.
-
xx - data.frame(market, price)
library(reshape2)
yy - melt(xx, id=1)
dcast(yy, market ~ variable, sum)
-

--- On Sun, 11/14/10, lgpeco pavic.o...@gmail.com wrote:

 From: lgpeco pavic.o...@gmail.com
 Subject: [R] sum in vector
 To: r-help@r-project.org
 Received: Sunday, November 14, 2010, 7:02 AM
 
 hy guys i have one question :)
 
 i have two vectors markets and price
 
 market - c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
 price - c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4,
 38, 96)
 i would like sum prices: market 1: (100+4+96), market 2:
 (38),..., market 9:
 (10+50)
 ao i would like get this result: (200, 38, ..., 60)
 and i don't wanna use while, for loops... is there any
 other function to sum
 these :)
 
 tnx for helping ;)
 -- 
 View this message in context: 
 http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041661.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@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.


__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread Henrique Dallazuanna
Try this:

 rowsum(price, market)

On Sun, Nov 14, 2010 at 10:02 AM, lgpeco pavic.o...@gmail.com wrote:


 hy guys i have one question :)

 i have two vectors markets and price

 market - c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
 price - c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4, 38, 96)
 i would like sum prices: market 1: (100+4+96), market 2: (38),..., market
 9:
 (10+50)
 ao i would like get this result: (200, 38, ..., 60)
 and i don't wanna use while, for loops... is there any other function to
 sum
 these :)

 tnx for helping ;)
 --
 View this message in context:
 http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041661.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread Feng Mai

to be more general you can also use aggregate:

aggregate(price,list(market),sum)


Henrique Dallazuanna wrote:
 
 Try this:
 
  rowsum(price, market)
 
 On Sun, Nov 14, 2010 at 10:02 AM, lgpeco pavic.o...@gmail.com wrote:
 

 hy guys i have one question :)

 i have two vectors markets and price

 market - c(1, 5, 7, 9, 9, 6, 5, 4, 4, 3, 1, 2, 1)
 price - c(100, 20, 30, 10, 50, 23, 23, 33, 96, 6, 4, 38, 96)
 i would like sum prices: market 1: (100+4+96), market 2: (38),..., market
 9:
 (10+50)
 ao i would like get this result: (200, 38, ..., 60)
 and i don't wanna use while, for loops... is there any other function to
 sum
 these :)

 tnx for helping ;)
 --
 View this message in context:
 http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041661.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.

 
 
 
 -- 
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O
 
   [[alternative HTML version deleted]]
 
 
 __
 R-help@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.
 
 

-- 
View this message in context: 
http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3041991.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread km
 Hi all,

 On Sun, Nov 14, 2010 at 10:06 PM, Henrique Dallazuanna www...@gmail.com 
 wrote:
 Try this:

  rowsum(price, market)
  does it work  with multiple factors/groups ?
  using multiple factors such as c(factor1,factor2,factor3,...) as
second arg with  rowsum() doesnt seem to work!
  am i missing something ?
  regards
  KM

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread lgpeco

thx all for help

this function works fine
aggregate(price,list(market),sum) 
i also find another one sapply(x, function)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/sum-in-vector-tp3041661p3042089.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] sum in vector

2010-11-14 Thread Henrique Dallazuanna
Try this:

rowsum(value, paste(factor1, factor2, factor3))

On Sun, Nov 14, 2010 at 5:21 PM, km srikrishnamo...@gmail.com wrote:

  Hi all,
 
  On Sun, Nov 14, 2010 at 10:06 PM, Henrique Dallazuanna 
 www...@gmail.com wrote:
  Try this:
 
   rowsum(price, market)
   does it work  with multiple factors/groups ?
   using multiple factors such as c(factor1,factor2,factor3,...) as
 second arg with  rowsum() doesnt seem to work!
   am i missing something ?
   regards
   KM

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.