David
David

Reputation: 115

grep not returning expected result in R

I'm trying to get back a list of field names from a data.frame in R that have "mean()" within them.

However, as an example, if I run:

grep("mean()", "fld_meanFreq()")

It returns an answer, i.e. 1, when I would expect 0.

When I do something similar on the list of field names, I'm getting field names containing both "mean()" and "meanFreq()".

How to fix please? I didn't see anything that dealt with this in a search on here.

Upvotes: 0

Views: 387

Answers (1)

quazgar
quazgar

Reputation: 4632

grep assumes that the first argument is a regular expression, if you don't supply the fixed=TRUE option. In your example, the parentheses in mean() stand for a subexpression (an empty one in this case), so your example is pretty equivalent to

grep("mean", "fld_meanFreq()")

Instead, try:

grep("mean()", "fld_meanFreq()", fixed=TRUE)

For more information about regular expressions, read the R help on regex.

Upvotes: 4

Related Questions