Suppose I have a dataframe 'd' defined as
L3 <- LETTERS[1:3]
d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
= TRUE))
(d <- d0[d0$fac %in% c('A', 'B'),])
x y fac
2 1 2 B
3 1 3 A
4 1 4 A
5 1 5 A
6 1 6 B
8 1 8 A
Even though factor 'fac' in 'd' only has 2 levels, but it seems
to bear the
birthmark of 3 levels from its parent 'd0':
str(d)
'data.frame': 6 obs. of 3 variables:
$ x : num 1 1 1 1 1 1
$ y : num 2 3 4 5 6 8
$ fac: Factor w/ 3 levels "A","B","C": 2 1 1 1 2
1
How can I cut the umbilical cord so that factor 'fac' in 'd'
would have an
accurate birth certificate with the correct number of levels? Apparently
the following does not work:
levels(d$fac) <- c('A', 'B')
Also any reason for this heritage?
Thanks,
Gang
[[alternative HTML version deleted]]
On 14.12.2013 21:09, Gang Chen wrote:> Suppose I have a dataframe 'd' defined as > > L3 <- LETTERS[1:3] > d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace > = TRUE)) > (d <- d0[d0$fac %in% c('A', 'B'),]) > > x y fac > 2 1 2 B > 3 1 3 A > 4 1 4 A > 5 1 5 A > 6 1 6 B > 8 1 8 A > > Even though factor 'fac' in 'd' only has 2 levels, but it seems to bear the > birthmark of 3 levels from its parent 'd0': > > str(d) > > 'data.frame': 6 obs. of 3 variables: > $ x : num 1 1 1 1 1 1 > $ y : num 2 3 4 5 6 8 > $ fac: Factor w/ 3 levels "A","B","C": 2 1 1 1 2 1d$fac <- factor(d$fac) or d$fac <- d$fac[,drop=TRUE] Best, Uwe Ligges> How can I cut the umbilical cord so that factor 'fac' in 'd' would have an > accurate birth certificate with the correct number of levels? Apparently > the following does not work: > > levels(d$fac) <- c('A', 'B') > > Also any reason for this heritage? > > Thanks, > Gang > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Dear Gang,
this seem to solve your problem.
http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r
best
daniel
________________________________________
Felad?: r-help-bounces at r-project.org [r-help-bounces at r-project.org] ;
meghatalmazó: Gang Chen [gangchen6 at gmail.com]
K?ldve: 2013. december 14. 21:09
To: r-help
T?rgy: [R] Change factor levels
Suppose I have a dataframe 'd' defined as
L3 <- LETTERS[1:3]
d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
= TRUE))
(d <- d0[d0$fac %in% c('A', 'B'),])
x y fac
2 1 2 B
3 1 3 A
4 1 4 A
5 1 5 A
6 1 6 B
8 1 8 A
Even though factor 'fac' in 'd' only has 2 levels, but it seems
to bear the
birthmark of 3 levels from its parent 'd0':
str(d)
'data.frame': 6 obs. of 3 variables:
$ x : num 1 1 1 1 1 1
$ y : num 2 3 4 5 6 8
$ fac: Factor w/ 3 levels "A","B","C": 2 1 1 1 2
1
How can I cut the umbilical cord so that factor 'fac' in 'd'
would have an
accurate birth certificate with the correct number of levels? Apparently
the following does not work:
levels(d$fac) <- c('A', 'B')
Also any reason for this heritage?
Thanks,
Gang
[[alternative HTML version deleted]]
______________________________________________
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.