0

I've downloaded a table from wikipedia and in some columns there are links next to numbers. Is this possible to delete it ?

In column in Rstudio it looks like this: 402[38]

[38] - this is what I don't want.

1 Answers1

1

We can do this easily in base R with Regex:

a <- data.frame(V1 = paste0(1:20, sprintf("[%s]", 50:70))

a$V2 <- gsub("\\[.*?\\]","", a$V1)

       V1 V2
1   1[50]  1
2   2[51]  2
3   3[52]  3
4   4[53]  4
5   5[54]  5
6   6[55]  6
7   7[56]  7
8   8[57]  8
9   9[58]  9
10 10[59] 10
11 11[60] 11
12 12[61] 12
13 13[62] 13
14 14[63] 14
15 15[64] 15
16 16[65] 16
17 17[66] 17
18 18[67] 18
19 19[68] 19
20 20[69] 20
21  1[70]  1

And this conveniently works for the case of multiple references as well:

a <- data.frame(V1 = paste0(1:20, sprintf("[%s][%s]", 50:70, 80:100)))
Mako212
  • 6,787
  • 1
  • 18
  • 37