Melissa Rosenkranz
2012-Apr-21 19:54 UTC
[R] using "factor" to eliminate unused levels without dropping other variables
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!!
Richard M. Heiberger
2012-Apr-21 20:57 UTC
[R] using "factor" to eliminate unused levels without dropping other variables
replace the line data.subset2 <- factor(data.subset1$Sample) with data.subset1$Sample <- factor(data.subset1$Sample) I assume your intention is to change the levels of the Sample variable in the data.subset1 data.frame. On Sat, Apr 21, 2012 at 3:54 PM, Melissa Rosenkranz < melissarosenkranz@gmail.com> wrote:> 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!! > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
la mer
2012-Apr-21 21:33 UTC
[R] using "factor" to eliminate unused levels without dropping other variables
That did the trick! Thank you much, Richard Heiberger. -- View this message in context: http://r.789695.n4.nabble.com/using-factor-to-eliminate-unused-levels-without-dropping-other-variables-tp4577123p4577206.html Sent from the R help mailing list archive at Nabble.com.