as.numeric with comma decimal separators?
This question already has an answer here:
as.numeric(sub(",", ".", Input, fixed = TRUE))
应该管用。
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
Also, if you are reading in the raw data, the read.table
and all the associated functions have a dec
argument. eg:
read.table("file.txt", dec=",")
When all else fails, gsub
and sub
are your friends.
上一篇: gsub用同一行代码替换和清理
下一篇: as.numeric用逗号分隔符?