【问题标题】:Decoding URL character string vector in R在R中解码URL字符串向量
【发布时间】:2013-11-20 09:33:54
【问题描述】:

假设您有一个包含 URL 编码字符串的原子向量。

例如:

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

有没有什么方法可以解码向量中的每个元素,返回一个与常规文本长度相同的向量?

换句话说,输出应该是:

c("im looking for uncle","im looking for sister")

基础 R 中的 URLdecode 不会矢量化,而且速度很慢。 R 之外有很多实用程序可以快速解码 URL 编码的字符串,但我在 R 中找不到任何好的实用程序。

【问题讨论】:

    标签: r url decode


    【解决方案1】:

    您可以使用sapply 将函数应用于向量。它将返回结果向量:

    > 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】:

      对于那些还不知道的人,urltools 包具有矢量化的 url_decodeurl_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"
      

      【讨论】:

        猜你喜欢
        • 2021-02-10
        • 1970-01-01
        • 2013-09-09
        • 2019-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        相关资源
        最近更新 更多