qfd
qfd

Reputation: 788

manipulating a string in R replacing decimals

i am a bit stuck on this problem. I am trying to delete the dots before the first number, but any dots between two numbers i would like to keep.

. . . . . . . . . . . . . . 122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)

for example the above should output to

122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)

I am not sure what functions or package i should be using to do the above

Thanks!

Upvotes: 0

Views: 52

Answers (2)

hwnd
hwnd

Reputation: 70750

This should work for what you're asking.

sub('^[\\h.]+', '', x, perl=TRUE)

Upvotes: 1

LyzandeR
LyzandeR

Reputation: 37889

Maybe something like this:

#copy pasted from your example
text <- ". . . . . . . . . . . . . . 122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"

#find the location of the first number using gregexpr
loc <- gregexpr('[0-9]', text)[[1]][1]

#substring the text from loc and until the end
substr(text, loc, nchar(text)) # or substring(text, loc)

Output:

[1] "122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"

Upvotes: 1

Related Questions