0

Suppose I have a very simple data frame:

symbol <- c("A", "A^","B","C","C^")

df = data.frame(symbol)

Now imagine this is a very large dataframe so that I cannot easily list the rows in which the character "^" appears. How can I subset the rows with (or without) that character?

Notice that things like:

df[grep("^", df$symbol)

or regular subsetting will not work since the carat "^" is usually used to represent the beggining of a string.

Many thanks

Jaap
  • 81,064
  • 34
  • 182
  • 193
Ignacio
  • 69
  • 4
  • 3
    you need to escape it , `df[grep("\\^", df$symbol),]` or `df[grep("^", df$symbol, fixed = TRUE),]` – Sotos Feb 21 '17 at 20:16
  • The `?regex` docs that introduce the carat also explain this escaping rule (in Sotos' comment). – Frank Feb 21 '17 at 20:17
  • Thanks! Its solved now! – Ignacio Feb 21 '17 at 20:17
  • Possible duplicate of [How do I deal with special characters like \^$.?\*|+()\[{ in my regex?](http://stackoverflow.com/questions/27721008/how-do-i-deal-with-special-characters-like-in-my-regex) – Jota Feb 21 '17 at 20:52

1 Answers1

1

Add fixed to the grep function, as follows:

df[grep("^", df$symbol, fixed=T),]
user3640617
  • 1,546
  • 13
  • 21