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
제휴하지 않습니다 StackOverflow