Reputation: 77
Suppose you have an atomic vector containing URL encoded character strings.
For example:
urlencoded<-c("im%20looking%20for%20uncle","im%20looking%20for%20sister")
Is there any way to decode every element in the vector, returning a vector of the same length with regular text?
In other words, the output should be:
c("im looking for uncle","im looking for sister")
URLdecode in base R doesn't vectorize and it's slow. There are lots of utilities outside of R that quickly decode URL encoded character strings, but I can't find any good utility in R.
Upvotes: 5
Views: 2916
Reputation: 214957
For those who don't yet know, there's urltools
package that has the vectorized url_decode
and url_encode
:
library(urltools)
urlencoded <- c("im%20looking%20for%20uncle","im%20looking%20for%20sister")
url_decode(urlencoded)
# [1] "im looking for uncle" "im looking for sister"
url_encode(c("im looking for uncle", "im looking for sister"))
# [1] "im%20looking%20for%20uncle" "im%20looking%20for%20sister"
Upvotes: 2
Reputation: 20045
You can apply a function to a vector with sapply
. It will return the result vector:
> urlencoded <- c("im%20looking%20for%20uncle", "im%20looking%20for%20sister")
> sapply(urlencoded, URLdecode, USE.NAMES = FALSE)
[1] "im looking for uncle" "im looking for sister"
Upvotes: 4