[R] elegant solution to transform vector into percentages?

jim holtman jholtman at gmail.com
Tue Jul 26 21:59:23 CEST 2005


use 'cut':

> store<-c(1,1.4,3,1.1,0.3,0.6,4,5)
> x.1 <- cut(store, breaks=c(-Inf,.8,1.2,Inf))
> table(x.1)/length(x.1)*100
x.1
(-Inf,0.8]  (0.8,1.2]  (1.2,Inf] 
        25         25         50 
> 

On 7/26/05, jhainm at fas.harvard.edu <jhainm at fas.harvard.edu> wrote:
> Hi,
> 
> I am looking for an elegant way to transform a vector into percentages of values
> that meet certain criteria.
> 
> store<-c(1,1.4,3,1.1,0.3,0.6,4,5)
> 
> # now I want to get the precentages of values
> # that fall into the categories <=M , >M & <=N , >N
> # let
> M <-.8
> N <- 1.2
> # In my real example I have many more of these cutoff-points
> 
> # What I did is:
> 
> out <- matrix(NA,1,3)
> 
>  out[1,1] <- ( (sum(store<=M                ))  /length(store) )*100
>  out[1,2] <- ( (sum(store> M  & store<= N   ))  /length(store) )*100
>  out[1,3] <- ( (sum(store> N                ))  /length(store) )*100
> 
> colnames(out)<-c("percent<=M","percent>M & <=N","percent>N")
> out
> 
> But this gets very tedious if I have many cutoff-points. Does anybody know a
> more elegant way to do this task?
> 
> Thanks so much.
> 
> Cheers,
> Jens
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 


-- 
Jim Holtman

What the problem you are trying to solve?




More information about the R-help mailing list