str_extract_all: return all patterns found in string concatenated as vector

前端 未结 1 922
悲哀的现实
悲哀的现实 2020-12-18 10:48

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

         


        
相关标签:
1条回答
  • 2020-12-18 11:26

    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))
    
    0 讨论(0)
提交回复
热议问题