Hello R
For expierienced user, the following problem will be easy to solve:
a<-c(0,1,0,1,0,2,3,4,3,2)
b<-c(3,3,3,4,4,4,7,7,7,10)
c<-data.frame(a,b)
Data Frame c contains tow colums. I would like to sum up all values in a as long
as b stays the same:
sum(a[which(b==1)])
does this, but i have to manually put in b
then i tryied st like this, but i canno't save it properly
for (i in 0:max(b)){
i<-sum(a[which(b==i)])
}
i tried to figure out, how tapply works, but neither
thanks alot
marc
--
marcg wrote:> Hello R > > For expierienced user, the following problem will be easy to solve: > > a<-c(0,1,0,1,0,2,3,4,3,2) > b<-c(3,3,3,4,4,4,7,7,7,10) > c<-data.frame(a,b) > > Data Frame c contains tow colums. I would like to sum up all values in a as long as b stays the same: > > sum(a[which(b==1)]) > > does this, but i have to manually put in b > > then i tryied st like this, but i canno't save it properly > > for (i in 0:max(b)){ > i<-sum(a[which(b==i)]) > } > > i tried to figure out, how tapply works, but neither> with(c, tapply(a, list(b), sum))3 4 7 10 1 3 10 2> thanks alot > > marc > -- > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Shubha Vishwanath Karanth
2007-Oct-23 12:08 UTC
[R] sum variable as long condition is true
rowsum(c$a,c$b)
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of marcg
Sent: Tuesday, October 23, 2007 5:22 PM
To: r-help at stat.math.ethz.ch
Subject: [R] sum variable as long condition is true
Hello R
For expierienced user, the following problem will be easy to solve:
a<-c(0,1,0,1,0,2,3,4,3,2)
b<-c(3,3,3,4,4,4,7,7,7,10)
c<-data.frame(a,b)
Data Frame c contains tow colums. I would like to sum up all values in a
as long as b stays the same:
sum(a[which(b==1)])
does this, but i have to manually put in b
then i tryied st like this, but i canno't save it properly
for (i in 0:max(b)){
i<-sum(a[which(b==i)])
}
i tried to figure out, how tapply works, but neither
thanks alot
marc
--
______________________________________________
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.
aggregate(c$a, list(b=b), sum) --- marcg <mdgi at gmx.ch> wrote:> Hello R > > For expierienced user, the following problem will be > easy to solve: > > a<-c(0,1,0,1,0,2,3,4,3,2) > b<-c(3,3,3,4,4,4,7,7,7,10) > c<-data.frame(a,b) > > Data Frame c contains tow colums. I would like to > sum up all values in a as long as b stays the same: > > sum(a[which(b==1)]) > > does this, but i have to manually put in b > > then i tryied st like this, but i canno't save it > properly > > for (i in 0:max(b)){ > i<-sum(a[which(b==i)]) > } > > i tried to figure out, how tapply works, but neither > > thanks alot > > marc > -- > > ______________________________________________ > 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. >