【问题标题】:Extract words between doubles quotes in a variable in R在R中的变量中提取双引号之间的单词
【发布时间】:2015-11-26 02:07:49
【问题描述】:

我想从以下输入中提取名称,其格式如括号中所示

# Example of the input in brackets('name":"Tale")
name<- c('name":"Tale"','name":"List"')

我想提取引号之间的名称,如下所示。有什么建议吗?

name
Tale
List

【问题讨论】:

    标签: regex r stringr stringi


    【解决方案1】:

    我们可以使用stri_extract_last_words

    library(stringi)
    library(data.table)
    setDT(list(name=stri_extract_last_words(name)))[]
    #   name
    #1: Tale
    #2: List
    

    【讨论】:

    • 从昨天开始,我一直在阅读 stringi 手册。你太快了,像往常一样。 :)
    • @jazzurro 感谢您的评论和投票。该软件包中有很多方便的功能。
    • 是的,我一直在使用其中一些来处理数据集的字符串提取。速度快。
    【解决方案2】:

    将向量转换为单列data.frame,然后只需使用gsub 从字符串中删除name":"

    例子:

    transform(data.frame(name), name = gsub("name\":|\"", "", name))
    ##   name
    ## 1 Tale
    ## 2 List
    

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多