I have a small annoying problem. When I use the 'table' function on a simple vector it counts the number of occurences. So depending on the values of my input vector the function returns a class of type table with different lengths. Is there an easy way to tell the table function, the values to expect? That is #############> set.seed(0) > s<-sample(0:5,5,rep=T) > s[1] 5 1 2 3 5> ts<-table(s) > tss 1 2 3 5 1 1 1 2 ############## And what I wanted was 0 1 2 3 4 5 0 1 1 1 0 2 Thanks
You can do it using factor: s0<-factor(0:5) s1<-sample(s0,5,re=T) s1 [1] 0 2 2 4 0 Levels: 0 1 2 3 4 5 table(s1) s1 0 1 2 3 4 5 2 0 2 0 1 0 Good luck! -- Marcio Pupin Mello Survey Engineer Ph.D student in Remote Sensing National Institute for Space Research (INPE) - Brazil Laboratory of Remote Sensing in Agriculture and Forestry (LAF) www.dsr.inpe.br/~mello Em 4/7/2011 12:37 AM, fisken escreveu:> I have a small annoying problem. > > When I use the 'table' function on a simple vector it counts the > number of occurences. > So depending on the values of my input vector the function returns a > class of type table with different lengths. > > Is there an easy way to tell the table function, the values to expect? > > That is > ############# >> set.seed(0) >> s<-sample(0:5,5,rep=T) >> s > [1] 5 1 2 3 5 >> ts<-table(s) >> ts > s > 1 2 3 5 > 1 1 1 2 > > ############## > > And what I wanted was > > 0 1 2 3 4 5 > 0 1 1 1 0 2 > > > Thanks >
Hi, You might try> table(factor(s, levels = 0:5))0 1 2 3 4 5 0 1 1 1 0 2 HTH, Jorge On Wed, Apr 6, 2011 at 11:37 PM, fisken <> wrote:> I have a small annoying problem. > > When I use the 'table' function on a simple vector it counts the > number of occurences. > So depending on the values of my input vector the function returns a > class of type table with different lengths. > > Is there an easy way to tell the table function, the values to expect? > > That is > ############# > > set.seed(0) > > s<-sample(0:5,5,rep=T) > > s > [1] 5 1 2 3 5 > > ts<-table(s) > > ts > s > 1 2 3 5 > 1 1 1 2 > > ############## > > And what I wanted was > > 0 1 2 3 4 5 > 0 1 1 1 0 2 > > > Thanks > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
On Thu, Apr 07, 2011 at 05:37:08AM +0200, fisken wrote:> When I use the 'table' function on a simple vector it counts the > number of occurences. > So depending on the values of my input vector the function returns a > class of type table with different lengths. > > Is there an easy way to tell the table function, the values to expect?> And what I wanted was > > 0 1 2 3 4 5 > 0 1 1 1 0 2The solution using factos has already been posted. if you are really interested in integers only you could also use tabulate():> tabulate(s)[1] 1 1 1 0 2 Note that this excludes zero, though. cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan Maximus-von-Imhof-Forum 3 85354 Freising, Germany http://webclu.bio.wzw.tum.de/~pagel/