简体   繁体   中英

Removing a particular category from a data frame in R

I have a single column in a data frame in R that looks something like this:

blue
green
blue
yellow
black
blue
green

How do I remove all the rows that indicate blue ? Please keep in mind that I don't want a NULL value represented in that row: I want the entire row removed.

Thank you :)

Also be careful about the difference between a factor variable and character vector .

Factors retain all original levels by default unless you reassign the altered vector as a new factor, or use one of the relevel functions.

> DF <- data.frame(v = factor(c("red", "blue", "green", "blue")))
> summary(DF)
     v    
 blue :2  
 green:1  
 red  :1  
> summary(DF[ DF$v != "blue", , drop=FALSE])
     v    
 blue :0  
 green:1  
 red  :1  
> DF <- DF[ DF$v != "blue", , drop=FALSE]; DF$v <- factor(DF$v); summary(DF)
     v    
 green:1  
 red  :1  
> 

What about

> df1 = data.frame(a=c("Red", "Blue", "Red"), b=1:3)
> df1[df1$a!= "Blue",]
    a b
1 Red 1
3 Red 3

If all those square brackets and commas and dollar signs confuse you, then why not try 'subset':

> d=data.frame(a=c("Red", "Blue", "Red"), b=1:3)
> subset(d,a!="Blue")
    a b
1 Red 1
3 Red 3
> Data[Data!="blue"]
[1] "green"  "yellow" "black"  "green"

or

> Data[which(Data!="blue",TRUE)]
[1] "green"  "yellow" "black"  "green"

Edit to respond to Joris' comment (this works for 1-column data.frames):

> str(Data)
'data.frame':   7 obs. of  1 variable:
 $ V1: Factor w/ 4 levels "black","blue",..: 2 3 2 4 1 2 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM