Search code examples
rstrsplit

Transform character string with ranges into numeric vector in R


I have a dataframe with a column containing ranges of values, as well as single values, like in the following:

x <- c("a","b")
y <- c("1:3,7","9")

dat <- data.frame("char"=x,"range"=y)

> dat
  char range
1    a 1:3,7
2    b     9

What I need to do is transform the range column so that all the values contained in the ranges show up:

> dat2
  char   range
1    a 1,2,3,7
2    b       9

I have made progress thanks to the solution provided in this very similar question:

v1 <- c("1:3")

unlist(lapply(v1, function(x) eval(parse(text=x))))

[1] 1 2 3 9

But this only gets me so far because of course if I pass a vector which also has single values, like my original range column, it will throw an error:

v1 <- c("1:3,7","9")

unlist(lapply(v1, function(x) eval(parse(text=x))))

Error in parse(text = x) : <text>:1:4: unexpected ','
1: 1:3,
^

In addition, I can't automatically apply this solution to the dataframe column, because it returns separate numeric values and I need a character string. Any help is appreciated.

EDIT
Thanks to everyone for the very useful comments!


Solution

  • The following modification gives you what you want...

    v1 <- c("1:3,7","9")
    
    unlist(lapply(v1, function(x) paste(eval(parse(text=paste0("c(",x,")"))), 
                                        collapse = ",")))
    
    [1] "1,2,3,7" "9"