Home > other >  Remove from vector elements containing a number in R
Remove from vector elements containing a number in R

Time:01-26

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?

CodePudding user response:

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.

CodePudding user response:

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"   

CodePudding user response:

"^(?!.*({{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" ```

CodePudding user response:

We can try this

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

> grep("^\\D $", v2, value = TRUE)
[1] "Buffalo" "Pass"    "Farm" 
  •  Tags:  
  • Related