I have a data.frame in which one column of numeric data is read by readr as character, at least in part because some of the values are "N/A". I don't know if the values actually include quotation marks.
I am trying to extract all the values in that column that contain things other than pure numbers, i.e. which contain a any character which not a number, 1-9. My purpose is to learn how many of these there are and to see if there are any formats besides the "N/A", in preparation for replacing them with something else and then converting the vector to numeric.
While I am confident that there are smarter ways to do this, I am trying to extract those values with a logical vector created from a regex applied to to the vector using R's grepl command.
A2 <- 1:10
A3 <- sample(1000:9999, 10)
dat_df <- data.frame(A2, A3)
str(dat_df)
dat_df$A3[1:3]<- c("N/A", "", "banana")
dat_df is a simplified data set, provided for reproducibility.
Here is an example
dat_df$A3[grepl(as.character(\<\d*[a-zA-Z][a-zA-Z0-9]*>\), x = dat_df$A3)]
This particular one gives the error
Error: unexpected '<' in dat_df"$A3[grepl(as.character(\<"
I have tried a lot of varients of this. These include:
Wrapping the initial data in ( ) (in case it was a precedence problem).
Defining the regex as a character string using as.character as the help file recommends, or with quotation marks.
Wrapping the central portion of the regex with ^ and $ instead of \< and >\
Doubling all the "\"s
In every case I get some variant of the syntax error shown above, varying with the version.
Error: unexpected (and then)
- '^' if it starts with a '^'
- '\' if it starts with a '\'
- '<' if it starts with a '<'
- '\d' if it is wrapped in quotation marks instead of using as.character
I can not make heads or tails out of this pattern of errors.
Any help gratefully received and acknowledged.