Matthew
Matthew

Reputation: 3

Trying to extract specific characters in a column in R?

The content in the column appears as follows $1,521+ 2 bds. I want to extract 1521 and put it in a new column. I know this can be done in alteryx using regex can I do it R?

Upvotes: 0

Views: 86

Answers (3)

akrun
akrun

Reputation: 887108

We can use sub from base R

as.numeric( sub("\\$(\\d+),(\\d+).*", "\\1\\2", x))
#[1] 1521

data

x <- '$1,521+ 2 bds' 

Upvotes: 0

Blue050205
Blue050205

Reputation: 276

How about the following?:

library(tidyverse)

x <- '$1,521+ 2 bds'

parse_number(x)

Upvotes: 1

jpdugo17
jpdugo17

Reputation: 7106

For example:

library(tidyverse)    

#generate some data
tbl <- tibble(string = str_c('$', as.character(seq(1521, 1541, 1)), '+', ' 2bds'))

new_col <- 
    tbl$string %>%
    str_split('\\+',simplify = TRUE) %>% 
    `[`(, 1) %>% 
    str_sub(2, -1) #get rid of '$' at the start

mutate(tbl, number = new_col)
#> # A tibble: 21 x 2
#>    string      number
#>    <chr>       <chr> 
#>  1 $1521+ 2bds 1521  
#>  2 $1522+ 2bds 1522  
#>  3 $1523+ 2bds 1523  
#>  4 $1524+ 2bds 1524  
#>  5 $1525+ 2bds 1525  
#>  6 $1526+ 2bds 1526  
#>  7 $1527+ 2bds 1527  
#>  8 $1528+ 2bds 1528  
#>  9 $1529+ 2bds 1529  
#> 10 $1530+ 2bds 1530  
#> # … with 11 more rows

Created on 2021-06-12 by the reprex package (v2.0.0)

Upvotes: 0

Related Questions