【问题标题】:How to extract the value inside the first parenthesis of a string and add it to another new column如何提取字符串第一个括号内的值并将其添加到另一个新列
【发布时间】:2021-05-19 06:27:34
【问题描述】:

我有一个数据框 (df),其中有一列 (Quantity) 包含字符串。

该列的摘录如下所示:

RoomType      Quantity

Comfort        Select rooms 0 1 (MUR 7,278) 2 (MUR 14,556) 3 (MUR 21,834) 4 (MUR 29,112) 5 (MUR 36,390) 6 (MUR 43,668) 7 (MUR 50,946) 8 (MUR 58,224) 9 (MUR 65,502) 10 (MUR 72,780)
Superior       Select rooms 0 1 (MUR 8,166) 2 (MUR 16,331) 3 (MUR 24,497) 4 (MUR 32,662) 5 (MUR 40,828) 6 (MUR 48,993) 7 (MUR 57,159) 8 (MUR 65,324) 9 (MUR 73,490) 10 (MUR 81,655)
...

我只需要提取数据框中每条记录的第一个括号内的数字,并将它们添加到新列(例如,DiscountedPrice)。

我想我需要在这里使用regex,但是在网上做了一些搜索后,我仍然无法弄清楚如何提取这些值。

我的R 代码如下:

library(dplyr)
library(stringr)

df %>% 
 mutate(DiscountedPrice = as.numeric(................) 

预期输出:

RoomType        Quantity     DiscountedPrice

Comfort        (as above)      7278
Superior       (as above)      8166

任何帮助将不胜感激。

【问题讨论】:

    标签: r regex stringr


    【解决方案1】:

    你可以使用 -

    df$DiscountedPrice <- as.numeric(gsub(',', '', 
                          sub('.*?\\(MUR (.*?)\\).*', '\\1', df$Quantity)))
    df$DiscountedPrice
    #[1] 7278 8166
    

    sub ('.*?\\(MUR (.*?)\\).*') 中的模式提取 MUR 之后直到右括号的值,gsub 从数字中删除逗号,as.numeric 将值更改为数字。

    数据

    df <- structure(list(RoomType = c("Comfort", "Superior"), Quantity = c("Select rooms 0 1 (MUR 7,278) 2 (MUR 14,556) 3 (MUR 21,834) 4 (MUR 29,112) 5 (MUR 36,390) 6 (MUR 43,668) 7 (MUR 50,946) 8 (MUR 58,224) 9 (MUR 65,502) 10 (MUR 72,780)", 
    "Select rooms 0 1 (MUR 8,166) 2 (MUR 16,331) 3 (MUR 24,497) 4 (MUR 32,662) 5 (MUR 40,828) 6 (MUR 48,993) 7 (MUR 57,159) 8 (MUR 65,324) 9 (MUR 73,490) 10 (MUR 81,655)"
    )), class = "data.frame", row.names = c(NA, -2L))
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2020-04-13
      • 2018-12-13
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多