Search code examples
rregexsplit

Splitting a string by commas, and removing some characters


Here are the values I would like to separate:

f <- data.frame(x = c("c(58663.809, 232648.355, 0)", "c(5902.873, 232674.248, 0)"))

I would like to remove the "c(", separate the two values (by the comma), and remove the final two characters "0)".

Two resulting columns should look like this:

x = c(58663.809, 5902.873),
y = c(232648.355, 232674.248)

Solution

  • Here is one option using dplyr and tidyr libraries.

    From the string x we remove "c(" and ")" characters, use separate_wider_delim to divide data in different columns while dropping the last value. Finally, use type.convert to change the column values to it's respective types.

    library(dplyr)
    library(tidyr)
    
    f %>%
      mutate(x = gsub("c\\(|\\)", "", x)) %>%
      separate_wider_delim(x, names = c("x", "y", NA), delim = ", ") %>%
      type.convert(as.is = TRUE) %>%
      data.frame()
    
    #          x        y
    #1 58663.809 232648.4
    #2  5902.873 232674.2