0

I'm trying to use readline to allow a user to input a numeric vector of indices to use, like c(1, 2, 10) or c(1:1300, 1500:2000), and I'm having trouble converting the character returned into a numeric vector. I saw in this post how to do that for a specifically enumerated vector like c(1, 2, 10), but it would be extremely impractical to use that method for something like c(1:1300, 1500:2000). Is there a generic way to do this that doesn't involve a bunch of conditional string splitting? Like, I can see how something like the pseudo-code below would work, it just seems like there should be a more natural method for this.

# pseudo-code
string.convert <- function(s){
    split0 = strip c() from s
    split1 = elements of split0 split by commas
    split2 = elements of split1 split by colons
    split3 = apply as.numeric to elements of split2
    split4 = insert integers between elements of split3 conditional on colon separation 
    return(concatenated results of split4)
}
Ben
  • 459
  • 4
  • 16

1 Answers1

1

Aaaand I just figured it out.

eval(parse(text='1:5'))

Ben
  • 459
  • 4
  • 16