I have a dataframe consisting of factors in one column. Im trying to remove certain levels using the following code: toBeRemoved1<-which(DATA$UnitName_1=="lake") DATA<-DATA[-toBeRemoved1,] However it will not remove the level "lake" In the past this worked for me, but its not working now. Any help appreciated. Thanks -- Shane [[alternative HTML version deleted]]
Am 13.06.2013 14:02, schrieb Shane Carey:> I have a dataframe consisting of factors in one column. Im trying to remove > certain levels using the following code: > toBeRemoved1<-which(DATA$UnitName_1=="lake") > DATA<-DATA[-toBeRemoved1,] > > However it will not remove the level "lake"Hello! Is this a part of the R Inferno? See "The R Inferno" from Patrick Burns, specially Chapter 8.2.4 "dropping factor levels ", page 83 http://www.burns-stat.com/pages/Tutor/R_inferno.pdf HTH, Albin -- | Albin Blaschka, Mag.rer.nat. | Etrichstrasse 26, A-5020 Salzburg | * www.albinblaschka.info * www.thinkanimal.info * | - It's hard to live in the mountains, hard but not hopeless!
Works fine for me. Too bad you didn't include actual data:> set.seed(42) > DATA <- data.frame(UnitName_1=factor(sample(c("lake", "pond","river"), + 15, replace=TRUE)), Var=sample.int(100, 15))> DATAUnitName_1 Var 1 river 95 2 river 97 3 lake 12 4 river 47 5 pond 54 6 pond 86 7 river 14 8 lake 92 9 pond 88 10 river 8 11 pond 99 12 river 35 13 river 80 14 lake 39 15 pond 72> toBeRemoved1 <- which(DATA$UnitName_1=="lake") > toBeRemoved1[1] 3 8 14> DATA <- DATA[-toBeRemoved1,] > DATAUnitName_1 Var 1 river 95 2 river 97 4 river 47 5 pond 54 6 pond 86 7 river 14 9 pond 88 10 river 8 11 pond 99 12 river 35 13 river 80 15 pond 72 ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shane Carey Sent: Thursday, June 13, 2013 7:03 AM To: r-help at r-project.org Subject: [R] Remove levels I have a dataframe consisting of factors in one column. Im trying to remove certain levels using the following code: toBeRemoved1<-which(DATA$UnitName_1=="lake") DATA<-DATA[-toBeRemoved1,] However it will not remove the level "lake" In the past this worked for me, but its not working now. Any help appreciated. Thanks -- Shane [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Hi, Without more information I guess your problem is that the level name still exists in the factor whereas it doesn't appear anymore in the factor. If so, try droplevels. Alain Guillet On 13/06/13 14:02, Shane Carey wrote:> I have a dataframe consisting of factors in one column. Im trying to remove > certain levels using the following code: > toBeRemoved1<-which(DATA$UnitName_1=="lake") > DATA<-DATA[-toBeRemoved1,] > > However it will not remove the level "lake" > > In the past this worked for me, but its not working now. Any help > appreciated. > > Thanks >-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain http://www.uclouvain.be/smcs Bureau c.316 Voie du Roman Pays, 20 (bte L1.04.01) B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50 Acc?s: http://www.uclouvain.be/323631.html