[R] two apparent anomalies
Berwin A Turlach
berwin at maths.uwa.edu.au
Sat Jan 22 15:50:24 CET 2011
On Sat, 22 Jan 2011 06:16:43 -0800 (PST)
"analyst41 at hotmail.com" <analyst41 at hotmail.com> wrote:
> (1)
>
> > a = c("a","b")
> > mode(a)
> [1] "character"
> > b = c(1,2)
> > mode(b)
> [1] "numeric"
> > c = data.frame(a,b)
> > mode(c$a)
> [1] "numeric"
R> str(c)
'data.frame': 2 obs. of 2 variables:
$ a: Factor w/ 2 levels "a","b": 1 2
$ b: num 1 2
Character vectors are turned into factors by default by data.frame().
OTOH:
R> c = data.frame(a,b, stringsAsFactors=FALSE)
R> mode(c$a)
[1] "character"
> (2)
>
>
> > a = c("a","a","b","b","c")
> > levels(as.factor(a))
> [1] "a" "b" "c"
> > levels(as.factor(a[1:3]))
> [1] "a" "b"
> > a = as.factor(a)
> > levels(a)
> [1] "a" "b" "c"
> > levels(a[1:3])
> [1] "a" "b" "c"
Subsetting factors does not get rid of no-longer used levels by default.
OTOH:
R> levels(a[1:3, drop=TRUE])
[1] "a" "b"
or
R> levels(factor(a[1:3]))
[1] "a" "b"
HTH.
Cheers,
Berwin
========================== Full address ============================
Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019) +61 (8) 6488 3383 (self)
The University of Western Australia FAX : +61 (8) 6488 1028
35 Stirling Highway
Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au
Australia http://www.maths.uwa.edu.au/~berwin
More information about the R-help
mailing list