2

I have a few files that are named after rural properties like the following:

v1 <- c("Badger", "Hill", "Farm", "1.json")
v2 <- c("Buffalo", "Pass", "Farm", "2.json")
> v1
[1] "Badger" "Hill"   "Farm"   "1.json"
> v2
[1] "Buffalo" "Pass"    "Farm"    "2.json"

I managed to split the file name elements, but I want to keep only those which do not contain any number on it. The desired output would be:

> v1
[1] "Badger" "Hill"   "Farm"  
> v2
[1] "Buffalo" "Pass"    "Farm"

Quite simple, but I just can't wrap my head around it. How can I achieve that?

pppery
  • 3,731
  • 22
  • 33
  • 46
thiagoveloso
  • 2,537
  • 3
  • 28
  • 57
  • 1
    Does this answer your question? [Remove numbers from alphanumeric characters](https://stackoverflow.com/questions/13590139/remove-numbers-from-alphanumeric-characters) – Maël Jan 25 '22 at 17:09
  • 1
    @Maël I don't think that's a very good duplicate target. Here the question author wants to remove elements that contain a number. In the duplicate target, the question author wants to remove the numbers but retain the rest of the character strings. I searched a bit and couldn't find a good duplicate. If you find one, ping me and I'll close this. [From Review](https://stackoverflow.com/review/close/30906571) – Ian Campbell Jan 25 '22 at 23:30

4 Answers4

5

This should do it:

v1 = v1[!grepl("[0-9]", v1)]
v2 = v2[!grepl("[0-9]", v2)]

grepl detects patterns, the regex pattern [0-9] will detect any digit.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
3

We can use str_subset from stringr

library(stringr)
str_subset(v1, "^\\D+$")
[1] "Badger" "Hill"   "Farm" 
str_subset(v2, "^\\D+$")
[1] "Buffalo" "Pass"    "Farm"   

Or in base R can specify invert = TRUE in grep

> grep("\\d", v1, invert = TRUE, value = TRUE)
[1] "Badger" "Hill"   "Farm"  
> grep("\\d", v2, invert = TRUE, value = TRUE)
[1] "Buffalo" "Pass"    "Farm"   
akrun
  • 874,273
  • 37
  • 540
  • 662
2

"^(?!.*({{STRING}}))" is a nice regex way of specifying not

v1 <- c("Badger", "Hill", "Farm", "1.json")
v2 <- c("Buffalo", "Pass", "Farm", "2.json")


grep("^(?!.*(\\d))", v1, value = TRUE, perl = TRUE)
## [1] "Badger" "Hill"   "Farm"  

grep("^(?!.*(\\d))", v1, value = TRUE, perl = TRUE)
## [1] "Badger" "Hill"   "Farm" ```
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
2

We can try this

> grep("^\\D+$", v1, value = TRUE)
[1] "Badger" "Hill"   "Farm"  

> grep("^\\D+$", v2, value = TRUE)
[1] "Buffalo" "Pass"    "Farm" 
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81