TSR TSR - 1 month ago 5
R Question

Replace space with %20 in r

I have following string:

url <- https://www.google.mu/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=green carot


I want to replace the space between green and carot with %20

>url
https://www.google.mu/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=green%20carot

Answer

Should this (for a straight sring replacement) be

> gsub(" ","%20"),url

Though, a URL encode function would be better