[R] Remove levels

David Carlson dcarlson at tamu.edu
Thu Jun 13 16:00:28 CEST 2013


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))
> DATA
   UnitName_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,]
> DATA
   UnitName_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.



More information about the R-help mailing list