How to find a specific part in a string and only save this part

What is the cleanest way to find, for example, the string ": [1-9] *" and save only this part?

You can work with regexec to get the starting points, but is there a cleaner way to get the value right away?

For instance:

test <- c("surface area: 458", "bedrooms: 1", "whatever") regexec(": [1-9]*", test) 

How do I get right away

 c(": 458",": 1", NA ) 
+6
source share
3 answers
 library(stringr) str_extract(test, ":.*") #[1] ": 458" ": 1" NA 

Or for a faster stringi approach

 library(stringi) stri_extract_first_regex(test, ":.*") #[1] ": 458" ": 1" NA 

If you need to keep the values ​​of one who does not have a match

 gsub(".*(:.*)", "\\1", test) #[1] ": 458" ": 1" "whatever" 
+7
source

You can use the R base, which handles this just fine.

 > x <- c('surface area: 458', 'bedrooms: 1', 'whatever') > r <- regmatches(x, gregexpr(':.*', x)) > unlist({r[sapply(r, length)==0] <- NA; r}) # [1] ": 458" ": 1" NA 

Although, it’s much easier for me to do this ...

 > x <- c('surface area: 458', 'bedrooms: 1', 'whatever') > sapply(strsplit(x, '\\b(?=:)', perl=T), '[', 2) # [1] ": 458" ": 1" NA 
+8
source

Try any of them. The first two use the base only R. The latter assumes that we want to return a numerical vector.

1) sub

 s <- sub(".*:", ":", test) ifelse(test == s, NA, s) ## [1] ": 458" ": 1" NA 

If there can be several: in the line replace the pattern "^[^:]*:" .

2) strsplit

 sapply(strsplit(test, ":"), function(x) c(paste0(":", x), NA)[2]) ## [1] ": 458" ": 1" NA 

Do not use this if there can be several: per line.

3) strapplyc

 library(gsubfn) s <- strapplyc(test, "(:.*)|$", simplify = TRUE) ifelse(s == "", NA, s) ## [1] ": 458" ": 1" NA 

We can omit the ifelse line if "" is ok, not NA .

4) strapply If the idea is really that there are some digits in the string and we want to return the numbers or NA, try the following:

 library(gsubfn) strapply(test, "\\d+|$", as.numeric, simplify = TRUE) ## [1] 458 1 NA 
+4
source

Source: https://habr.com/ru/post/974618/


All Articles