删除R中的括号 [英] Removing parenthesis in R

查看:125
本文介绍了删除R中的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我试图从字符串值中删除括号:

I am trying to remove parentheses from a string value in this case this one:

(40.703707008,-73.943257966)

(40.703707008, -73.943257966)

我似乎找不到包含有效代码的帖子;我知道这是一个非常简单的任务,但是我看过以下链接,但是它们要么杀死了我所有的标点符号,要么似乎不起作用.以下是我尝试过的代码.感谢帮助:

I can't seem to find a post with code that works; I know that this is a very simple task, but I've seen the following links but they either kill all my punctuation or don't seem to work. Below is the codes I've tried. Appreciate the help:

从字符串中删除括号

从R中的字符串中删除括号和文本

x = ("(40.703707008, -73.943257966)")
gsub("\\s*\\([^\\)]+\\)","",x)
gsub("\\D", "", x)
gsub("log\\(", "", x)

推荐答案

这些是需要转义的元字符(使用\\),或者我们可以将其放在方括号中以将其读取为字符.

These are metacharacters that either needs to be escaped (with \\) or we can place it in a square bracket to read it as character.

gsub("[()]", "", x)
#[1] "40.703707008, -73.943257966"

这篇关于删除R中的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆