Re: [R] How to make a table of a desired dimension

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Fri, 08 Jun 2007 18:49:59 +0100

You need to basically use table on factors with fixed pre-specified levels. For example:

  x <- c(runif(100,10,40), runif(100,43,55))
  y <- c(runif(100,7,35),  runif(100,37,50))
  z <- c(runif(100,10,42), runif(100,45,52))
  xx <- ceiling(x); yy <- ceiling(y); zz <- ceiling(z)

  mylevels <- min( c(xx, yy, zz) ) : max( c(xx, yy, zz) )

  out <- cbind( table( factor(xx, levels=mylevels) ),
                table( factor(yy, levels=mylevels) ),
                table( factor(zz, levels=mylevels) ) )

You could replace the last command with simply

  sapply( list(xx, yy, zz),

                function(vec) table( factor(vec, levels=mylevels) ) )

Regards, Adai

Rubén Roa-Ureta wrote:
> Hi ComRades,
>
> I want to make a matrix of frequencies from vectors of a continuous
> variable spanning different values. For example this code
> x<-c(runif(100,10,40),runif(100,43,55))
> y<-c(runif(100,7,35),runif(100,37,50))
> z<-c(runif(100,10,42),runif(100,45,52))
> a<-table(ceiling(x))
> b<-table(ceiling(y))
> c<-table(ceiling(z))
> a
> b
> c
>
> will give me three tables that start and end at different integer
> values, and besides, they have 'holes' in between different integer
> values. Is it possible to use 'table' to make these three tables have
> the same dimensions, filling in the absent labels with zeroes? In the
> example above, the desired tables should all start at 8 and tables 'a'
> and 'c' should put a zero at labels '8' to '10', should all put zeros in
> the frequencies of the labels corresponding to the holes, and should all
> end at label '55'. The final purpose is the make a matrix and use
> 'matplot' to plot all the frequencies in one plot, such as
>
> #code valid only when 'a', 'b', and 'c' have the proper dimension
> p<-mat.or.vec(48,4)
> p[,1]<-8:55
> p[,2]<-c(matrix(a)[1:48])
> p[,3]<-c(matrix(b)[1:48])
> p[,4]<-c(matrix(c)[1:48])
> matplot(p)
>
> I read the help about 'table' but I couldn't figure out if dnn,
> deparse.level, or the other arguments could serve my purpose. Thanks for
> your help
> Rubén
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
>
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Fri 08 Jun 2007 - 17:59:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Jun 2007 - 18:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.