字符串上数字之间的差异

时间:2014-08-22 11:07:33

标签: r

我有一个载体

 numbers <- c("25", "22|9", "2|5", "23|7" , "25|15")

我需要找到&#34; |&#34;

左侧和右侧数字之间的差异

结果应为

c(25, 13, -3, 16, 10)

3 个答案:

答案 0 :(得分:4)

如果你曾经告诉我,我将使用eval(parse()),我会嘲笑我的屁股,但现在就是。

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 

话虽如此:

> 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)

答案 1 :(得分:3)

Aaaaannd获胜者:

numin <- rep(c("25", "22|9", "2|5", "23|7" , "25|15"),1000)
roman <- function(numbers){
numbers <- gsub("\\|", "-", numbers)
sapply(numbers, FUN = function(x) eval(parse(text = x)))
}

akrun<-function(numbers){
with(read.table(text=gsub("^(\\d+)$", "\\1|0", numbers), sep="|", header=FALSE), V1-V2)
}
arenburg<-function(numbers){
temp <- strsplit(numbers, "[|]")
sapply(temp, function(x) {x <- as.numeric(x) ; if(!is.na(x[2])) x[1] - x[2] else x[1]} )
}
    aren2 <-function(numbers){
as.numeric(gsubfn("([0-9]+)[|]([0-9]+)", ~ as.numeric(x) - as.numeric(y), numbers))
}

microbenchmark(roman(numin),akrun(numin),arenburg(numin),aren2(numin),times=3)
Unit: milliseconds
            expr       min         lq     median        uq
    roman(numin)  306.9893  325.70831  344.42732  352.0125
    akrun(numin)   17.7421   17.94138   18.14065   18.1941
 arenburg(numin)  222.0295  222.91272  223.79597  228.9837
    aren2(numin) 2214.5777 2244.93384 2275.28998 2320.8854
        max neval
  359.59769     3
   18.24756     3
  234.17140     3
 2366.48072     3

更新:以上是在Windows7-64,R3.1.1上我再次在OSX SnowLeopard,Intel Core2 Duo,R3.1.0上运行,并且

Unit: milliseconds
            expr       min         lq     median         uq       max neval
    roman(numin)  604.2358  605.65310  607.07037  617.16463  627.2589     3
    akrun(numin)  103.3000  103.56248  103.82494  104.89133  105.9577     3
 arenburg(numin)   36.8553   39.40521   41.95512   41.95746   41.9598     3
    aren2(numin) 2976.1608 3052.62308 3129.08530 3206.96755 3284.8498     3

所有这一切都清楚akrun与arenburg的合作方式/原因。

答案 2 :(得分:2)

试用gsubfn

library(gsubfn)
as.numeric(gsubfn("([0-9]+)[|]([0-9]+)", ~ as.numeric(x) - as.numeric(y), numbers))
## [1] 25 13 -3 16 10

strsplit解决方案

temp <- strsplit(numbers, "[|]")
sapply(temp, function(x) {x <- as.numeric(x) ; if(!is.na(x[2])) x[1] - x[2] else x[1]})
## [1] 25 13 -3 16 10