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?
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) , ]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With