Kaja
Kaja

Reputation: 3057

Delete special character from a string value

I have such a value:

" 0.67564;"

and I want to delete ; and only have the double value. how can I do it in r?

Upvotes: 1

Views: 92

Answers (3)

Prasanna Nandakumar
Prasanna Nandakumar

Reputation: 4335

I would probably use

as.numeric(gsub("[^[:alnum:]///' ]", "", " 0.67564;"))

Can also be done using regex

[^[:alnum:]] is preferred to [^0-9A-Za-z]. ?regex says "because their interpretation is locale- and implementation-dependent, they are best avoided." and "For example, [[:alnum:]]means [0-9A-Za-z], except the latter depends upon the locale and the character encoding, whereas the former is independent of locale and character set

Upvotes: 1

Mikhail
Mikhail

Reputation: 1028

parseFloat() will return exactly what you need

Upvotes: 0

tonytonov
tonytonov

Reputation: 25608

a <- " 0.67564;"
gsub(';', '', a)
[1] " 0.67564"

To get a numeric representation:

as.numeric(gsub(';', '', a))
[1] 0.67564

Upvotes: 3

Related Questions