Hello, A stupid question: I have an array with two columns, the first "a" acting as my index in 0.25 steps, the second one "b" the column of interest. How can i sum up "b" only for a specified window in "a" (as the window command for time series) a=seq(0,10,0.25) b=runif(41) c=data.frame(a,b) Sum up c if 3<a<5.25 How to do that? thanks marc -- Psssst! Schon vom neuen GMX MultiMessenger geh?rt? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
d <- subset(c, c$a > 3 & c$a < 5.25 ) sum(d[,2]) --- mdgi at gmx.ch wrote:> Hello, > > A stupid question: > > I have an array with two columns, the first "a" > acting as my index in 0.25 steps, the second one "b" > the column of interest. How can i sum up "b" only > for a specified window in "a" (as the window command > for time series) > > a=seq(0,10,0.25) > b=runif(41) > c=data.frame(a,b) > > Sum up c if 3<a<5.25 > > How to do that? thanks > > marc > -- > Psssst! Schon vom neuen GMX MultiMessenger geh?rt? > Der kann`s mit allen: > http://www.gmx.net/de/go/multimessenger > > ______________________________________________ > 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. >
sum(c$b[(c$a > 3) & (c$a < 5.25)]) On 11/7/07, mdgi at gmx.ch <mdgi at gmx.ch> wrote:> Hello, > > A stupid question: > > I have an array with two columns, the first "a" acting as my index in 0.25 steps, the second one "b" the column of interest. How can i sum up "b" only for a specified window in "a" (as the window command for time series) > > a=seq(0,10,0.25) > b=runif(41) > c=data.frame(a,b) > > Sum up c if 3<a<5.25 > > How to do that? thanks > > marc > -- > Psssst! Schon vom neuen GMX MultiMessenger geh?rt? > Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger > > ______________________________________________ > 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 you are trying to solve?
There's probably a shorter way but below works and doesn't require the starting points to be in the a column. startindex<-3 endindex<-5.25 start<-tail(which(c$a<=startindex),1) end<-tail(which(c$a<=endindex),1) sum(c$b[start:end])>Hello, > >A stupid question: > >I have an array with two columns, the first "a" acting as my index in 0.25 steps, the second one "b" the column of interest. How can i sum up "b" only for a specified window in "a" (as the window command for time series) > >a=seq(0,10,0.25) >b=runif(41) >c=data.frame(a,b) > >Sum up c if 3<a<5.25 > >How to do that? thanks > >marc >-- >Psssst! Schon vom neuen GMX MultiMessenger geh??rt? >Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger > >______________________________________________ >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.
I'm assuming that you want to add b if 3<a<5.25. If so, there are many ways. One of them is sum (b[a>3 & a<5.25]) This is very simple R coding. I recommend you spend some time learning the basics. There are very good tutorials at the R website. Julian mdgi at gmx.ch wrote:> Hello, > > A stupid question: > > I have an array with two columns, the first "a" acting as my index in 0.25 steps, the second one "b" the column of interest. How can i sum up "b" only for a specified window in "a" (as the window command for time series) > > a=seq(0,10,0.25) > b=runif(41) > c=data.frame(a,b) > > Sum up c if 3<a<5.25 > > How to do that? thanks > > marc