[R] summing up and cut off with looping

Bert Gunter bgunter.4567 at gmail.com
Fri Jun 10 21:44:54 CEST 2016


1. Please read the posting guide (link below) to learn how to post
understandable questions -- at least I was not able to understand. In
particular, post in plain text, not html, which tends to get mangled
as seemed to occur here.

2. Your first stop in learning R should be one of the many fine
tutorials available on the web or even the "Intro to R" tutorial that
ships with R.  In particular, indexing in R using logical expressions
appears relevant to your query. Here is an example of what can be done
along the lines that I think you asked about -- hope it helps.
Apologies if I have misunderstood. See also ?subset and ?cumsum .


> set.seed(1021)

> y <- runif(10)

> y
 [1] 0.36751828 0.08721951 0.08899027 0.38838635 0.33331978 0.72948251
 [7] 0.36669151 0.28457792 0.90614056 0.31832515

> y[cumsum(y) < 1.2]
[1] 0.36751828 0.08721951 0.08899027 0.38838635


Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jun 10, 2016 at 12:27 PM, oslo via R-help <r-help at r-project.org> wrote:
> Hi all;
> I am quite new in R. I have tried write a loop to sum up a column and cut off when summation reach certain point. Here are a small example and my R codes.
> Your helps are truly appreciated,
> Oslo
> file=AposA posB1     92     75     124     79     13.    . .    .
> File=Bpos  a  b   c    4   .4  7  .82   .1  5  .47  .5  8  .321   .4  1  .113  .1  6  .1312  .2  11 .019   .3  12 .23.    .   .  ..    .   .  .
> I would like to sum up the B$a column and cut off at 0.7 for the each row of intervals giving in file=A.For example the interval  at the first row in A$posA and A$posB is 1 and 9. So, I need adding up the B$a and cut off B$a>.7 from the 1 to 9 in B$pos. And then I need to the same using the intervals in the second, third..... rows in A. Obviously my loop is wrong and  does not work properly. Please help for my this first experience.  Thanks.
> Here are my codes
> #sorting B$possort=B[order(B$pos),]
> #Running loop
> for(i in 1:nrow(A)) {if(sum(B[a$B, i:A[1:2])>0.7) {print(A[1:i,]) } }
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list