add-semi-colons
add-semi-colons

Reputation: 18810

R: grep drop all columns even when its not matching

Trying to remove columns from a large data frame. Using grep and it works fine when actually there are matching columns. But when there is zero matching columns it drops all the columns.

s <- s[, -grep("^Test", colnames(s))]

To confirm that there are no columns that match Test

> y <- grep("^Test", colnames(s))
> y
integer(0)

What is exactly going on here?

Upvotes: 1

Views: 229

Answers (1)

Mako212
Mako212

Reputation: 7292

You need to use grepl and ! instead.

df2 <- data.frame(ID =c(1,2,3), T = c("words", "stuff","things"))

df2[,!grepl("^Test", colnames(df2))]

  ID      T
1  1  words
2  2  stuff
3  3 things

-grep() or -grepl() return integer(0) when there isn't a match.

-TRUE == -1 where as !TRUE == FALSE

Using !grepl() returns the full logical vector (TRUE TRUE) for each column header, allowing you to correctly subset when no columns meet the condition. In other words for colname(df)[i], grepl(..., colnames(df))[i] returns TRUE where your pattern is matched, then using ! you invert to keep the values that don't match, and remove the ones that do.

Upvotes: 2

Related Questions