[R] two apparent anomalies

Moritz Grenke r-list at 360mix.de
Sat Jan 22 16:12:05 CET 2011


My explanation for No2: 

When coercing a character vector to factor, the current levels are stored.
By choosing a subvector of the factor you don't change the levels of the
factor. So levels(a[1:3]) is still [1] "a" "b" "c" in the last line ... 

If you want to reduce levels you need to tell R. 

> levels(a[1:3, drop=TRUE])
[1] "a" "b"

________________
Moritz Grenke
http://www.360mix.de

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von analyst41 at hotmail.com
Gesendet: Samstag, 22. Januar 2011 15:17
An: r-help at r-project.org
Betreff: [R] two apparent anomalies

(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"

(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"

Any explanation would be helpful.  Thanks.

______________________________________________
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