我知道关于正则表达式的堆栈溢出有很多问题,但是我无法通过已有的帮助来完成这项简单的任务。这是我的数据:

a<-c("Los Angeles, CA","New York, NY", "San Jose, CA")
b<-c("c(34.0522, 118.2437)","c(40.7128, 74.0059)","c(37.3382, 121.8863)")

df<-data.frame(a,b)
df
                a                    b
1 Los Angeles, CA c(34.0522, 118.2437)
2    New York, NY  c(40.7128, 74.0059)
3    San Jose, CA c(37.3382, 121.8863)

我想删除除数字和句点以外的所有内容(即删除“c”,“)”和“(”。这是到目前为止我已经尝试过的操作:
str_replace(df$b,"[^0-9.]","" )
[1] "(34.0522, 118.2437)" "(40.7128, 74.0059)"  "(37.3382, 121.8863)"

str_replace(df$b,"[^\\d\\)]+","" )
[1] "34.0522, 118.2437)" "40.7128, 74.0059)"  "37.3382, 121.8863)"

不知道还剩下什么可以尝试。我想得出以下结论:
 [1] "34.0522, 118.2437" "40.7128, 74.0059"  "37.3382, 121.8863"

谢谢。

最佳答案

试试这个

gsub("[\\c|\\(|\\)]", "",df$b)
#[1] "34.0522, 118.2437" "40.7128, 74.0059"  "37.3382, 121.8863"

09-05 11:19