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.
tapply(price, market, sum) andydolman at gmail.com On 14 November 2010 13:02, lgpeco <pavic.olic at 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 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. >
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
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.olic at gmail.com> wrote:> From: lgpeco <pavic.olic at gmail.com> > Subject: [R] sum in vector > To: r-help at 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 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. >
Try this: rowsum(price, market) On Sun, Nov 14, 2010 at 10:02 AM, lgpeco <pavic.olic@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]]
Hi all,>> >> On Sun, Nov 14, 2010 at 10:06 PM, Henrique Dallazuanna <wwwhsd at 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
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.