I have a vector
numbers <- c("25", "22|9", "2|5", "23|7" , "25|15")
I need to find the difference between numbers on the left and right of "|"
The result should be
c(25, 13, -3, 16, 10)
If you ever told me I'll be using eval(parse()), I would have laughed my ass off, yet here it is.
numbers <- c("25", "22|9", "2|5", "23|7" , "25|15")
numbers <- gsub("\\|", "-", numbers)
sapply(numbers, FUN = function(x) eval(parse(text = x)))
25 22-9 2-5 23-7 25-15
25 13 -3 16 10
That being said:
> require(fortunes)
> fortune("parse")
Personally I have never regretted trying not to underestimate my own future stupidity.
-- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering a question triggered by the infamous fortune(106))
R-help (January 2007)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With