[R] extracting the levels of a subset of data

Douglas Bates bates at stat.wisc.edu
Fri Sep 19 14:49:24 CEST 2003


Another alternative to trim the levels of a factor is to use drop =
TRUE.

> mydata = data.frame(TYPE=factor(rep(LETTERS[1:3], 8:6)),y=rnorm(21))
> tmpdata = subset(mydata, TYPE=="A")
> levels(tmpdata$TYPE)
[1] "A" "B" "C"
> tmpdata$TYPE = tmpdata$TYPE[, drop = TRUE]
> levels(tmpdata$TYPE)
[1] "A"


Jesus Frias <Jesus.Frias at dit.ie> writes:

> Hiya
> 
> > I'd like to get only "A" as output...
> 
> two solutions:
> 1.-use unique()
> 
> 	unique(tempdata$TYPE)
> 
> to get your answer
> 
> 
> 2.-or call factor again to reduce the levels:
> > tmpd <- subset(myd,clas=="A")
> > levels(tmpd$clas)
> [1] "A" "B" "C"
> > tmpd$clas <- factor(tmpd$clas)
> > levels(tmpd$clas)
> [1] "A"
> 
> 
> regards,
> 
> IOsu
> 
>  
> 
> -- 
> This message has been scanned for content and 
> viruses by the DIT ICT Services MailScanner Service,  
> and is believed to be clean.
> http://www.dit.ie
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/




More information about the R-help mailing list