[R] factor level issue after subsetting

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Nov 1 22:51:58 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Schreiber, Stefan
> Sent: Tuesday, November 01, 2011 2:29 PM
> To: r-help at r-project.org
> Subject: [R] factor level issue after subsetting
> 
> Dear list,
> 
> I cannot figure out why, after sub-setting my data, that particular
> item
> which I don't want to plot is still in the newly created subset (please
> see example below). R somehow remembers what was in the original data
> set. 

That is the nature of factors.  Once created, unused levels must be xplicitly dropped

plot(droplevels(dat.sub$treat),dat.sub$yield)


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204



A work around is exporting and importing the new subset. Then it's
> all fine; but I don't like this idea and was wondering what am I
> missing
> here?
> 
> Thanks!
> Stefan
> 
> P.S. I am using R 2.13.2 for Mac.
> 
> > dat<-read.csv("~/MyFiles/data.csv")
> > class(dat$treat)
> [1] "factor"
> > dat
>    treat yield
> 1   cont  98.7
> 2   cont  97.2
> 3   cont  96.1
> 4   cont  98.1
> 5     10 103.0
> 6     10 101.3
> 7     10 102.1
> 8     10 101.9
> 9     30 121.1
> 10    30 123.1
> 11    30 119.7
> 12    30 118.9
> 13    60 109.9
> 14    60 110.1
> 15    60 113.1
> 16    60 112.3
> > plot(dat$treat,dat$yield)
> > dat.sub<-dat[which(dat$treat!='cont')]
> > class(dat.sub$treat)
> [1] "factor"
> > dat.sub
>    treat yield
> 5     10 103.0
> 6     10 101.3
> 7     10 102.1
> 8     10 101.9
> 9     30 121.1
> 10    30 123.1
> 11    30 119.7
> 12    30 118.9
> 13    60 109.9
> 14    60 110.1
> 15    60 113.1
> 16    60 112.3
> > plot(dat.sub$treat,dat.sub$yield)
> 
> 	[[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.



More information about the R-help mailing list