我知道关于正则表达式的堆栈溢出有很多问题但我无法通过我见过的可用帮助来完成这一个简单的任务.这是我的数据:
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,121.8863"