K Owen
K Owen

Reputation: 1250

remove rows containing certain data

In my data frame the first column is a factor and I want to delete rows that have a certain value of factorname (when the value is present). I tried:

df <- df[-grep("factorname",df$parameters),]

Which works well when the targeted factor name is present. However if the factorname is absent, this command destroys the data frame, leaving it with 0 rows. So I tried:

df <- df[!apply(df, 1, function(x) {df$parameters == "factorname"}),]

that does not remove the offending lines. How can I test for the presence of factorname and remove the line if factorname is present?

Upvotes: 4

Views: 25722

Answers (2)

Metrics
Metrics

Reputation: 15458

l<-sapply(iris,function(x)is.factor(x)) # test for the factor variables
>l
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
       FALSE        FALSE        FALSE        FALSE         TRUE 

m<-iris[,names(which(l=="TRUE"))]) #gives the data frame of factor variables only
iris[iris$Species !="setosa",] #generates the data with Species other than setosa 



   > head(iris[iris$Species!="setosa",])
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
51          7.0         3.2          4.7         1.4 versicolor
52          6.4         3.2          4.5         1.5 versicolor
53          6.9         3.1          4.9         1.5 versicolor
54          5.5         2.3          4.0         1.3 versicolor
55          6.5         2.8          4.6         1.5 versicolor
56          5.7         2.8          4.5         1.3 versicolor

Upvotes: 2

IRTFM
IRTFM

Reputation: 263481

You could use:

df[ which( ! df$parameter %in% "factorname") , ]

(Used %in% since it would generalize better to multiple exclusion criteria.) Also possible:

df[ !grepl("factorname", df$parameter) , ]

Upvotes: 7

Related Questions