[R] using "factor" to eliminate unused levels without dropping other variables

Melissa Rosenkranz melissarosenkranz at gmail.com
Sat Apr 21 21:54:53 CEST 2012


Hello,

I have been banging my head against the wall trying to figure out this
seemingly simple problem with no success. I'm hoping that one or some
of you can help.

Here is the code I am trying to use:

#importing data
data.file <-read.csv("/file/location", header=TRUE, sep = ",")
#selecting a subset of data based on variable "Sample"
data.subset1 <- subset(data.file,  subset=(Sample !='sample_x' &
Sample !='sample_y')).

**This leaves me a data file that has 8 levels of the variable
"Sample" and 2 empty levels that correspond to sample_x and sample_y.
I need to get rid of these two levels for plotting purposes, so I
tried using the code below...

data.subset2 <- factor(data.subset1$Sample)

**This does get rid of the two empty levels, but also gets rid of all
the other variables in the dataset, which I want to keep. So my
question is, how do I accomplish getting rid of the empty levels,
while retaining the other variables in the dataset?? I tried the
drop.levels option of subset, but couldn't get it to do what I need to
do.

Many thanks!!



More information about the R-help mailing list