【问题标题】:How to extract a substring from a column of strings if it matches a given value?如果匹配给定值,如何从字符串列中提取子字符串?
【发布时间】:2019-09-08 02:23:29
【问题描述】:

如果与特定值匹配,我正在尝试从字符串中为列中的每一行提取一个子字符串。随后我想将匹配的子字符串添加到新列中。子字符串必须匹配 'GET'、'POST' 或 'HEAD'。字符串采用以下格式:'GET /icons/circle_logo_small.gif'。

这是我当前的数据框

这是预期的数据帧

【问题讨论】:

标签: r substring


【解决方案1】:

您需要提取space 之前的所有字符串。 一种方式:

df$Category <- gsub( " .*$", "", df$request )

假设 df 是您的数据框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2020-11-11
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2016-05-16
    • 2020-09-20
    • 2023-03-17
    相关资源
    最近更新 更多