0

I have the following vector in R:

myvec <- c("1.80 m (5 ft 11 in)[2]", "1.81 m (5 ft 11 1⁄2 in)[2]", "1.86 m (6 ft 1 in)[2]", "1.75 m (5 ft 9 in)[2][3][4]")

I want to remove anything that starts with "[", inclusive, so that I end up with:

myvec_clean <- c("1.80 m (5 ft 11 in)", "1.81 m (5 ft 11 1⁄2 in)", "1.86 m (6 ft 1 in)", "1.75 m (5 ft 9 in)")

I've tried the following but it's only removing the first case:

gsub("\\[[\\d-]\\]+", "", myvec) 
Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

Try:

gsub("\\[[^\\]]+\\]", "", myvec, perl=T)

This removes anything inside square brackets, including the brackets. It should yield the following:

# [1] "1.80 m (5 ft 11 in)"     "1.81 m (5 ft 11 1⁄2 in)" "1.86 m (6 ft 1 in)"      "1.75 m (5 ft 9 in)" 

I hope this helps.

Abdou
  • 12,931
  • 4
  • 39
  • 42