【问题标题】:Selecting following characters after a pattern match in R在R中的模式匹配后选择以下字符
【发布时间】:2020-07-13 18:25:23
【问题描述】:

我有一个数据框,其中有一列包含我需要提取的信息的文本,这是该列的一个观察结果:每个问题都有与其相关的三个属性目标、KeyResults 和责任

[{"text":"Newideas.","translationKey":"new.question-4","id":4,"objectives":"Great","KeyResults":"Awesome","responsible":"myself"},{"text":"customer focus.","translationKey":"new.question-5","id":5,"objectives":"Goalset","KeyResults":"Amazing","responsible":"myself"}

--------------所需的输出-------- ---

Question#   Objectives KeyResults responsible Question#   Objectives KeyResults responsible 
4            Great      Awesome    myself       5           Goalset    Amazin     myself

【问题讨论】:

  • 请使用dput展示示例
  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则会更容易为您提供帮助。

标签: r regex string split


【解决方案1】:

Data 是一个有效的json(但您需要用方括号将] 括起来)。您可以使用 json 解析器包将 json 读入 R 对象(例如jsonlite

假设您的文本位于数据框 df 的列 text 中,那么这会将文本转换为 R 数据框。

library(jsonlite)

dat <- fromJSON(df$text)
dat

#              text translationKey id objectives KeyResults responsible
# 1       Newideas. new.question-4  4      Great    Awesome      myself
# 2 customer focus. new.question-5  5    Goalset    Amazing      myself

您需要安装jsonlite 才能正常工作

install.packages("jsonlite")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2020-06-22
    相关资源
    最近更新 更多