Hi
suppose I have a factor 'x':
> x <- as.factor(c(rep("a",3),"b","d"))
> table(x)
x
a b d
3 1 1
>
>
But this is not what I want because
I need to include the fact that the count of "c" is zero.
I can't just change the levels of x:
> levels(x) <- c("a","b","c","d")
> table(x)
x
a b c d
3 1 1 0
>
because this records the single "d" in the original 'x' as a
"c".
What I want is:
a b c d
3 1 0 1
How to get this from 'x'?
(my real application has dozens of levels with complicated names).
--
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877
On Tue, 2010-06-29 at 11:59 +0100, Robin Hankin wrote:> Hi > > suppose I have a factor 'x': > > > x <- as.factor(c(rep("a",3),"b","d")) > > table(x) > x > a b d > 3 1 1 > > > > > > But this is not what I want because > I need to include the fact that the count of "c" is zero. > > I can't just change the levels of x: > > > levels(x) <- c("a","b","c","d") > > table(x) > x > a b c d > 3 1 1 0 > > > > because this records the single "d" in the original 'x' as a "c". > > > What I want is: > > a b c d > 3 1 0 1If you know the levels before hand (which you appear to do) then state them when you create the factor, but use factor() rather than coerce with as.factor(): x <- factor(c(rep("a",3),"b","d"), levels = letters[1:4])> table(x)x a b c d 3 1 0 1> > How to get this from 'x'? > (my real application has dozens of levels with complicated names). > > >-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
You could try
x<- factor(c(rep("a",3),"b","d"),
levels=letters[1:4])
table(x)
# x
# a b c d
# 3 1 0 1
Hope this helps
Allan
On 29/06/10 11:59, Robin Hankin wrote:> Hi
>
> suppose I have a factor 'x':
>
> > x <- as.factor(c(rep("a",3),"b","d"))
> > table(x)
> x
> a b d
> 3 1 1
> >
> >
>
> But this is not what I want because
> I need to include the fact that the count of "c" is zero.
>
> I can't just change the levels of x:
>
> > levels(x) <-
c("a","b","c","d")
> > table(x)
> x
> a b c d
> 3 1 1 0
> >
>
> because this records the single "d" in the original 'x'
as a "c".
>
>
> What I want is:
>
> a b c d
> 3 1 0 1
>
>
> How to get this from 'x'?
> (my real application has dozens of levels with complicated names).
>
>
>
Just use factor(), not levels(); you can pass a factor to factor() too.> x <- factor(c(rep("a",3),"b","d"), levels = letters[1:5]) > table(x)x a b c d e 3 1 0 1 0 Cheers, -Felix On 29 June 2010 20:59, Robin Hankin <rksh1 at cam.ac.uk> wrote:> Hi > > suppose I have a factor 'x': > >> x <- as.factor(c(rep("a",3),"b","d")) >> table(x) > x > a b d > 3 1 1 >> >> > > But this is not what I want because > I need to include the fact that the count of "c" is zero. > > I can't just change the levels of x: > >> levels(x) <- c("a","b","c","d") >> table(x) > x > a b c d > 3 1 1 0 >> > > because this records the single "d" in the original 'x' as a "c". > > > What I want is: > > a b c d > 3 1 0 1 > > > How to get this from 'x'? > (my real application has dozens of levels with complicated names). > > > > -- > Robin K. S. Hankin > Uncertainty Analyst > University of Cambridge > 19 Silver Street > Cambridge CB3 9EP > 01223-764877 > > ______________________________________________ > 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. >-- Felix Andrews / ??? Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andrews at anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix/