Thanks for the fast response and your comments!

That works perfect!

 

Another little mystery solved ;)

 

Stefan

 

 

From: Felipe Carrillo [mailto:mazatlanmex...@yahoo.com] 
Sent: Tuesday, November 01, 2011 3:54 PM
To: Schreiber, Stefan; r-help@r-project.org
Subject: Re: [R] factor level issue after subsetting

 

Stefan:

Use the droplevels function...

dat <- read.table(textConnection("
  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"),header=T)
dat
 plot(dat$treat,dat$yield)
 dat.sub <- subset(dat,treat!="cont");dat.sub
 dat.sub <- droplevels(dat.sub)    # drop unwanted levels
plot(dat.sub$treat,dat.sub$yield)

 

Felipe D. Carrillo

Supervisory Fishery Biologist

Department of the Interior

US Fish & Wildlife Service

California, USA

http://www.fws.gov/redbluff/rbdd_jsmp.aspx

         

        From: "Schreiber, Stefan" <stefan.schrei...@ales.ualberta.ca>
        To: r-help@r-project.org
        Sent: Tuesday, November 1, 2011 2:28 PM
        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. 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@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.
        
        


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to