giac
giac

Reputation: 4309

R - gsub only digits

I would like to clean this vector and only retain the digits

vec = c(" 4010  \"Filling in time budget diary\"", " 8888  \"Prob cont. preceding activity\"", " 9999   \"Missing, undecipherable\";") 

what I would like is simply : 4010, 8888, 9999

I thought of something like, matching exactly the digits but it doesn't work.

gsub("^[[:digit:]]$", replacement = '', vec)

Thanks

Upvotes: 5

Views: 12295

Answers (1)

akrun
akrun

Reputation: 887691

We can use \\D+ to match all non-numeric elements and replace with ''

 gsub('\\D+','', vec)

Upvotes: 16

Related Questions