如何从列表中删除特定字符并另存为数据框?

时间:2019-02-05 19:33:47

标签: r list dataframe text

我想

  • 获取干净的数据集
  • 没有特殊字符
  • 仅包含实际单词
  • 没有数字
  • 来自JSON文件
  •  library(rvest); library(XML); library(dplyr);library(tidyr); library(purrr); library(rjson)
    
     url <- "http://suggestqueries.google.com/complete/search?client=chrome&q=Nike"
     nike_autocomplete <- read_html(url)
    

    输出应如下所示:

    [1] "Nike" "nike air" "nike air max" "nike schuhe" "nike air force" "nike air max 97"
    [7] "nike tn" "nike id" "nike air max 270" "nike vapormax" "nike pullover" "nike schweiz"   
    [13] "nike 97" "nike off white" "nike air max plus" "nike winterschuhe" "nike schuhe damen" "nike huarache"  
    [19] "nike shoes" "nike logo" "nike air max 90"
    

    因此,最后没有空的东西

    1 个答案:

    答案 0 :(得分:1)

    您要提取的文本为JSON格式,因此使用json读取实用程序比尝试使用正则表达式要好得多。我喜欢jsonlite::fromJSON

    library(rvest)
    library(jsonlite)
    library(purrr)
    
    url <- "http://suggestqueries.google.com/complete/search?client=chrome&q=Nike"
    read_html(url) %>%
      xml_text %>%
      fromJSON() %>%
      extract(1:2) %>%
      unlist
    #  [1] "Nike"                  "nike shox"             "nike shoes"            "nike air max"         
    #  [5] "nike outlet"           "nike air force 1"      "nike basketball shoes" "nike vapormax"        
    #  [9] "nike air max 97"       "nike id"               "nike store"            "nike stock"           
    # [13] "nike air max 270"      "nike promo code"       "nike windbreaker"      "nike sweatshirts"     
    # [17] "nike huarache"         "nike hoodie"           "nike cortez"           "nike sweatpants"      
    # [21] "nike slides"