简体   繁体   中英

Put all the numbers in a string to the end in R

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!!

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"

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(str_extract_all(x, "\\d+")[[1]], 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

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 "

Using str_remove and str_extract is another option (easy to read/grasp):

library(stringr)
library(dplyr)

df |>
  mutate(a = paste(str_remove(a, "\\d+ "), str_extract(a, "\\d+")))

Output:

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

Data:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM