as.numeric with comma decimal separators?

前端 未结 7 1688
野性不改
野性不改 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:03

    Building on @adibender solution:

    input = '23,67'
    as.numeric(gsub(
                    # ONLY for strings containing numerics, comma, numerics
                    "^([0-9]+),([0-9]+)$", 
                    # Substitute by the first part, dot, second part
                    "\\1.\\2", 
                    input
                    ))
    

    I guess that is a safer match...

提交回复
热议问题