as.numeric with comma decimal separators?

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

    The answer by adibender does not work when there are multiple commas.

    In that case the suggestion from use554546 and answer from Deena can be used.

    Input = c("1,223,765", "122,325,000", "23,054")
    as.numeric(gsub("," ,"", Input))
    

    ouput:

    [1] 1223765 122325000 23054
    

    The function gsub replaces all occurances. The function sub replaces only the first.

提交回复
热议问题