Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove rows containing certain data

Tags:

dataframe

r

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?

like image 385
K Owen - Reinstate Monica Avatar asked Jan 13 '23 16:01

K Owen - Reinstate Monica


2 Answers

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) , ]
like image 54
IRTFM Avatar answered Jan 16 '23 00:01

IRTFM


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
like image 35
Metrics Avatar answered Jan 16 '23 01:01

Metrics