[R] Remove levels

arun smartpink111 at yahoo.com
Thu Jun 13 16:07:42 CEST 2013


Hi,
Not sure if the OP is concerned about this:
Using David's data

 str(DATA)
#'data.frame':    15 obs. of  2 variables:
# $ UnitName_1: Factor w/ 3 levels "lake","pond",..: 3 3 1 3 2 2 3 1 2 3 ...
 #$ Var       : int  95 97 12 47 54 86 14 92 88 8 ...
 toBeRemoved1 <- which(DATA$UnitName_1=="lake")
 DATA <- DATA[-toBeRemoved1,]
 str(DATA)
#'data.frame':    12 obs. of  2 variables:
# $ UnitName_1: Factor w/ 3 levels "lake","pond",..: 3 3 3 2 2 3 2 3 2 3 ...
# $ Var       : int  95 97 47 54 86 14 88 8 99 35 ...
 levels(DATA[,1])
#[1] "lake"  "pond"  "river" ############
 DATA[,1]<- factor(DATA[,1])
 str(DATA)
#'data.frame':    12 obs. of  2 variables:
# $ UnitName_1: Factor w/ 2 levels "pond","river": 2 2 2 1 1 2 1 2 1 2 ...
# $ Var       : int  95 97 47 54 86 14 88 8 99 35 ...
 levels(DATA[,1])
#[1] "pond"  "river"


A.K.



----- Original Message -----
From: David Carlson <dcarlson at tamu.edu>
To: 'Shane Carey' <careyshan at gmail.com>; r-help at r-project.org
Cc: 
Sent: Thursday, June 13, 2013 10:00 AM
Subject: Re: [R] Remove levels

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.

______________________________________________
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