[R] how to save this result in a vector

David Winsemius dwinsemius at comcast.net
Mon Nov 1 01:46:47 CET 2010


On Oct 31, 2010, at 8:35 PM, Changbin Du wrote:

> HI, Dear R community,
>
> I have the following codes to calculate the commulative coverage.

Not sure exactly what you mean by this. My guess is implemented below.

> I want to
> save the output in a vector, How to do this?
>
> test<-seq(10, 342, by=2)
>
> #cover is a vector
> cover_per<-function (cover) {
> for (i in min(cover):max(cover))

Using either for (i in cover) { ...} or for (i in seq_along(cover) )  
{...} would be more typical.

> {print(100*sum(ifelse(cover >= i, 1,
> 0))/length(cover))}
> }
>
> result<-cover_per(test)

Are you looking for cumsum?

 > test<-seq(10, 34, by=2)
 > 100*cumsum(test)/sum(test)
  [1]   3.496503   7.692308  12.587413  18.181818  24.475524   
31.468531  39.160839
  [8]  47.552448  56.643357  66.433566  76.923077  88.111888 100.000000

 > print(100*cumsum(test)/sum(test), digits=2)
  [1]   3.5   7.7  12.6  18.2  24.5  31.5  39.2  47.6  56.6  66.4   
76.9  88.1 100.0

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list