split and unite columns in R

how can i transform the first data frame into the second with regex expressions or any other way

  df <- data.frame (element  = c("Autres produits d'exploitation 918,306"),V2 = c("2"), V3=c("676")
  newdf <- data.frame (element  = c("Autres produits d'exploitation"),V2 = c("2 676 918,306")

Created on 2021-05-16 by the reprex package (v1.0.0.9002)

This is one way to do it


df <- data.frame (
    element  = c("Autres produits d'exploitation 918,306"),
    V2 = c("2"), 

df %>% 
    mutate(temp = str_extract(element, "[\\d,]+$")) %>% 
    transmute(element = str_remove(element, "[\\d,]+$"),
              v2 = paste(V2, V3, temp))
#>                           element            v2
#> 1 Autres produits d'exploitation  2 676 918,306

Created on 2021-05-16 by the reprex package (v2.0.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.