ATMathew ATMathew - 25 days ago 8
R Question

Removing Brackets From a String

I have a character vector in which each element is enclosed in brackets. I want
to remove the brackets and just have the string.

So I tried:

n = c("[Dave]", "[Tony]", "[Sara]")

paste("", n, "", sep="")


Unforunately, this doesn't work for some reason.
I've performed the same task before using this same code, and am not sure why it's not working this time.

I want to go from
'[Dave]'
to
'Dave'
.

What am I doing wrong?

Answer

You could gsub out the brackets like so:

n = c("[Dave]", "[Tony]", "[Sara]")

gsub("\\[|\\]", "", n)
[1] "Dave" "Tony" "Sara"