Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何提取字符串第一个括号内的值并将其添加到另一个新列_R_Regex_Stringr - Fatal编程技术网

R 如何提取字符串第一个括号内的值并将其添加到另一个新列

R 如何提取字符串第一个括号内的值并将其添加到另一个新列,r,regex,stringr,R,Regex,Stringr,我有一个数据帧(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) Su

我有一个数据帧(
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
任何帮助都将不胜感激。

您可以使用-

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