Decoding a character string character string in R

Suppose you have an atomic vector containing a string of characters encoded by a URL.

For example:

urlencoded<-c("im%20looking%20for%20uncle","im%20looking%20for%20sister") 

Is there a way to decode every element in a vector by 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")

The URL in the R database is not vectorized, and it is slow. There are many utilities outside of R that quickly decode URL encoded character strings, but I cannot find any useful utility in R.

+4
source share
2 answers

You can apply the function to the vector with sapply. It will return a 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"
+2
source

, , urltools, url_decode 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"
+1

All Articles