[R] other way of making a table?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Oct 9 15:33:33 CEST 2012


On 09/10/2012 9:24 AM, Jessica Streicher wrote:
> So..
>
> 		real=factor(realLabels)
> 		predicted=factor(predictedLabels)
> 		fl<-unique(levels(real),levels(predicted))
> 		real=factor(realLabels,fl)
> 		predicted=factor(pl,fl)
> 		table(real,predicted)
>
> ?
>
> i kinda dont like it :/

Why make it so complicated?  Don't you know the levels in advance? If 
so, it's much simpler:

levels <- c(0,1)
x <- factor( c(1,1,1,0,0), levels=levels)
y <- factor( c(1,1,1,1,1), levels=levels)
table(x,y)

Even if you don't know them, the levels calculation doesn't need to work 
on a factor, you could simply do

x<-c(1,1,1,0,0)
y<-c(1,1,1,1,1)
levels <- unique(c(x,y))
x <- factor( x, levels=levels)
y <- factor( y, levels=levels)

table(x,y)

Duncan Murdoch



>
>
> On 09.10.2012, at 15:00, Jeff Newmiller wrote:
>
> > Use factors?
> > ---------------------------------------------------------------------------
> > Jeff Newmiller                        The     .....       .....  Go Live...
> > DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
> >                                      Live:   OO#.. Dead: OO#..  Playing
> > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> > /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> > ---------------------------------------------------------------------------
> > Sent from my phone. Please excuse my brevity.
> >
> > Jessica Streicher <j.streicher at micromata.de> wrote:
> >
> >> I'm making tables for prediction results of classifiers (2 classes)
> >> that show the usual numbers, true positives, false positives, etc
> >>
> >> I used the command
> >>
> >> table(predictedLabels,realLabels)
> >>
> >> to make those.
> >>
> >> I just had a case though ,where one of the label vectors had only one
> >> class in it. This will result in only half a table.
> >>
> >> Compare:
> >> x<-c(1,1,1,0,0)
> >> y<-c(1,1,1,0,1)
> >> table(x,y)
> >>
> >> to
> >>
> >> x<-c(1,1,1,0,0)
> >> y<-c(1,1,1,1,1)
> >> table(x,y)
> >>
> >> I want the second one to still have all 4 cases (second column all
> >> zeros then).
> >>
> >> Any easy solutions?
> >>
> >> ______________________________________________
> >> R-help at 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.
> >
>
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list