HI, David, Juan, and Joshua,

Thanks so much for your help!  The following codes works. Appreciated!

test<-seq(10, 342, by=2)

#data is a vector
cover_per<-function (data) {

output<-vector("numeric",length(min(data):max(data)))

    for (i in min(data):max(data)) {
          output[i]<-(100*sum(ifelse(data >= i, 1, 0))/length(data))
                                              }

return(output)

}


result<-cover_per(test)

#***************************************************

#data is a vector
cover_per<-function (data) {

output<-numeric(0)
for (i in min(data):max(data)) {
          x<-(100*sum(ifelse(data >= i, 1, 0))/length(data))
          output<-c(output, x)
                               }

return(output)
}


result<-cover_per(test)














On Sun, Oct 31, 2010 at 5:46 PM, David Winsemius <dwinsem...@comcast.net>wrote:

>
> 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
>
>


-- 
Sincerely,
Changbin
--

Changbin Du
DOE Joint Genome Institute

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to