5

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.

2 Answers 2

4

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"
Sign up to request clarification or add additional context in comments.

Comments

2

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.