as.numeric with comma decimal separators?

前端 未结 7 1707
野性不改
野性不改 2020-11-27 04:32

I have a large vector of strings of the form:

Input = c(\"1,223\", \"12,232\", \"23,0\")

etc. That\'s to say, decimals separated by commas,

7条回答
  •  忘掉有多难
    2020-11-27 05:23

    scan(text=Input, dec=",")
    ## [1]  1.223 12.232 23.000
    

    But it depends on how long your vector is. I used rep(Input, 1e6) to make a long vector and my machine just hangs. 1e4 is fine, though. @adibender's solution is much faster. If we run on 1e4, a lot faster:

    Unit: milliseconds
             expr        min         lq     median         uq        max neval
      adibender()   6.777888   6.998243   7.119136   7.198374   8.149826   100
     sebastianc() 504.987879 507.464611 508.757161 510.732661 517.422254   100
    

提交回复
热议问题