【问题标题】:Need to Separate a String in R using grepl function需要使用 grepl 函数在 R 中分隔字符串
【发布时间】:2020-07-16 21:31:38
【问题描述】:

我有一个这样的列,但我需要将重量数字与筹码名称分开。使用 grepl() 函数。

productWords[grepl(pattern = "![:digit:]", x= productWords)]

它不工作:/

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。您的代码被粘贴为图片,因此人们很难为您提供帮助。此外,错误消息将有助于了解。请考虑在此处查看如何在stackoverflow.com/help/minimal-reproducible-example 上制作可重现的示例以及其他提示here

标签: r grepl


【解决方案1】:

stringr 包试试这个

x <- "Natural Chip 175g"
stringr::str_extract(x, '[0-9]{3}g')

如果权重可以超过 3 位,试试这个 -

stringr::str_extract(x, '[0-9]+g')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多