jhainm@fas.harvard.edu
2005-Jul-26 19:48 UTC
[R] elegant solution to transform vector into percentages?
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
Huntsinger, Reid
2005-Jul-26 19:57 UTC
[R] elegant solution to transform vector into percentages?
hist() or cut() followed by tabulate() would probably be the ingredients you'd want. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jhainm at fas.harvard.edu Sent: Tuesday, July 26, 2005 3:49 PM To: r-help at stat.math.ethz.ch Subject: [R] elegant solution to transform vector into percentages? 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
2005-Jul-26 19:59 UTC
[R] elegant solution to transform vector into percentages?
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)*100x.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?
Marc Schwartz (via MN)
2005-Jul-26 20:18 UTC
[R] elegant solution to transform vector into percentages?
On Tue, 2005-07-26 at 15:48 -0400, 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, > JensSomething alone the lines of: store <- c(1, 1.4, 3, 1.1, 0.3, 0.6, 4, 5) M <- 0.8 N <- 1.2 x <- hist(store, br = c(min(store), M, N, max(store)), plot = FALSE)$counts pct.x <- prop.table(x) * 100 names(pct.x) <- c("percent <= M","percent > M & <= N","percent > N")> pct.xpercent <= M percent > M & <= N percent > N 25 25 50 I think that should do it. See ?hist for more information and take note of the 'include.lowest' and 'right' arguments relative to whether or not values are or are not included in the specified intervals. See ?prop.table as well. Also be acutely aware of potential problems with exact equality comparisons with floating point numbers and the break points...if you have a float value equal to a breakpoint in your vector. HTH, Marc Schwartz
Adaikalavan Ramasamy
2005-Jul-26 22:21 UTC
[R] elegant solution to transform vector into percentages?
Why not write a function ? Here is one. mytable <- function(x, br){ n <- length(br) tb <- table( cut( x, breaks=c(-Inf, br, Inf) ) ) tb <- 100 * tb / sum(tb) tb.n <- paste( c("", br), c(br, ""), sep=" < x <= " ) tb.n[1] <- paste("x <= ", br[1], sep="") tb.n[n+1] <- paste("x > ", br[n], sep="") names(tb) <- tb.n return(tb) } mytable( store, br=c(0.8, 1.2) ) x <= 0.8 0.8 < x <= 1.2 x > 1.2 25 25 50 You might want to do a bit more testing especially at the break points. However I do not like the output of the above because the names of the table overlaps. Here is another function that might have nicer output. mytable2 <- function(x, br){ tb <- as.matrix( mytable( x=x, br=br ) ) colnames(tb) <- "Percentage" return(tb) } mytable2( store, br=c(0.8, 1.2) ) Percentage x <= 0.8 25 0.8 < x <= 1.2 25 x > 1.2 50 Hope this helps. Regards, Adai On Tue, 2005-07-26 at 15:48 -0400, 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 >