ATMathew ATMathew - 1 year ago 176
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

What am I doing wrong?

Answer Source

You could gsub out the brackets like so:

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

gsub("\\[|\\]", "", n)
[1] "Dave" "Tony" "Sara"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download