Oliver Bandel
2008-Nov-16 13:00 UTC
[R] Changing values (factors) does not change levels of that value?!
Hello, * I read in a server weblog with read.table. -> OK. * I look for the downloaded-size-values (filesize of the download) -> OK * I found "-" and wanted to substitute them with "0" and used: weblog$V8[ weblog$V8 == "-" ] <- 0 -> OK * checked the contents on "-" vs. "0" and found all "-" substituted by 0 -> OK * when then looking at str(weblog), the "-" will stay in the levels, mentioned for the variable weblog$V8 -> BAD! Is this snormal behaviour? Do I have to throw out the unwanted level by myself? Ciao, Oliver
Philipp Pagel
2008-Nov-16 13:35 UTC
[R] Changing values (factors) does not change levels of that value?!
> * when then looking at str(weblog), > the "-" will stay in the levels, mentioned for the variable weblog$V8 > -> BAD! > > Is this snormal behaviour?Yes, it is. The idea is that a factor has a given set of levels independent of how often you find them in your data - including the case that a level is not observed at all. E.g. gender cn take levels 'male' or 'female' but you may have a sample of females.> Do I have to throw out the unwanted level by myself?Yes, and it's easy:> x <- factor(c('A','B','C','A','C')) > y <- x[x!='C'] > y[1] A B A Levels: A B C> factor(y)[1] A B A Levels: A B cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel