Home > Back-end >  Put all the numbers in a string to the end in R
Put all the numbers in a string to the end in R

Time:01-27

the dataset that I work contains some numbers (usually up to 12) and I need to have all those numbers at the end:

# A tibble: 2 x 1
  a                   
  <chr>               
1 THIS IS 1 AN EXAMPLE
2 THIS 2 IS AN EXAMPLE

I tried doing sth like this with gsub but it doesn't work as I want:

df <- df %>%
  dplyr::mutate_at(.vars=vars(a), list(~ gsub(" (\\d) ", "\\2 \\1", .)))

Gives me this:

    A tibble: 2 x 1
  a                  
  <chr>              
1 THIS IS 1AN EXAMPLE
2 THIS 2IS AN EXAMPLE

What I want is: THIS IS AN EXAMPLE 1, THIS IS AN EXAMPLE 2.

How can I do this? Any help is appreciated!!

CodePudding user response:

You can use gregexpr and regmatches.

s <- c("THIS IS 1 AN EXAMPLE", "THIS 2 IS AN EXAMPLE", "THIS 2 IS AN 3 EXAMPLE")

x <- gregexpr(" *\\d ", s)
y <- regmatches(s, x)
regmatches(s, x) <- ""
paste0(s, sapply(y, paste0, collapse = ""))
#[1] "THIS IS AN EXAMPLE 1"   "THIS IS AN EXAMPLE 2"   "THIS IS AN EXAMPLE 2 3"

CodePudding user response:

With parse_number

library(readr)
library(dplyr)

df <- tibble(a = c("THIS IS 1 AN EXAMPLE", "THIS 2 IS AN EXAMPLE"))

df %>% 
  mutate(a = paste(sub("\\d  ", "", a), parse_number(a)))
# A tibble: 2 × 1
  a                   
  <chr>               
1 THIS IS AN EXAMPLE 1
2 THIS IS AN EXAMPLE 2

If you have more numbers using stringr

library(dplyr)
library(stringr)

df <- tibble(a = c("THIS IS 1 AN EXAMPLE", "THIS 2 IS AN EXAMPLE",
 "THIS 223 IS AN 3 EXAMPLE"))

df %>% 
  mutate(a = paste(gsub("\\d  ", "", a), sapply(a, function(x) 
    paste(unlist(str_extract_all(x, "\\d ")), collapse=" "))))
# A tibble: 3 × 1
  a                       
  <chr>                   
1 THIS IS AN EXAMPLE 1    
2 THIS IS AN EXAMPLE 2    
3 THIS IS AN EXAMPLE 223 3

CodePudding user response:

Should be quite simple if you aim to detect all parts in gsub pattern using three separate brackets for pre-match, match and post-match parts:

library(tidyverse)

tibble(a = c("THIS IS 1 AN EXAMPLE", "THIS 2 IS AN EXAMPLE")) |> 
  mutate(a = gsub("(.*)( \\d )(.*)", "\\1 \\3\\2", a))
#> # A tibble: 2 × 1
#>   a                      
#>   <chr>                  
#> 1 "THIS IS AN EXAMPLE 1 "
#> 2 "THIS IS AN EXAMPLE 2 "
  • Related