Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str_extract_all: return all patterns found in string concatenated as vector

Tags:

r

dplyr

stringr

I want to extract everything but a pattern and return this concetenated in a string.

I tried to combine str_extract_all together with sapply and cat

x = c("a_1","a_20","a_40","a_30","a_28")
data <- tibble(age = x)


# extracting just the first pattern is easy
data %>% 
  mutate(age_new = str_extract(age,"[^a_]"))
# combining str_extract_all and sapply doesnt work
data %>% 
  mutate(age_new = sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep="")))


class(str_extract_all(x,"[^a_]"))
sapply(str_extract_all(x,"[^a_]"),function(x) cat(x,sep=""))

Returns NULL instead of concatenated patterns

like image 634
MatzeKnop Avatar asked Jul 16 '19 14:07

MatzeKnop


2 Answers

Instead of cat, we can use paste. Also, with tidyverse, can make use of map and str_c (in place of paste - from stringr)

library(tidyverse)
data %>% 
  mutate(age_new = map_chr(str_extract_all(x, "[^a_]+"), ~ str_c(.x, collapse="")))

using `OP's code

data %>%
    mutate(age_new = sapply(str_extract_all(x,"[^a_]"),
               function(x) paste(x,collapse="")))

If the intention is to get the numbers

library(readr)
data %>%
     mutate(age_new = parse_number(x))
like image 151
akrun Avatar answered Oct 19 '22 19:10

akrun


Here is a non tidyverse solution, just using stringr.

apply(str_extract_all(column,regex_command,simplify = TRUE),1,paste,collapse="")

'simplify' = TRUE changed str_extract_all to output a matrix, and apply iterates over the matrix. I got the idea from https://stackoverflow.com/a/4213674/8427463

Example: extract all 'r' in rownames(mtcar) and concatenate as a vector

library(stringr)
apply(str_extract_all(rownames(mtcars),"r",simplify = TRUE),1,paste,collapse="")
like image 1
jf2017 Avatar answered Oct 19 '22 18:10

jf2017