简体   繁体   中英

removing data within specific delimiters in R

I have some data where the cells in one particular column look like this:

[SOME.LAST.NAME, SOME.FIRST.NAME 1] SOME.ADDRESS 1; [SOME.LAST.NAME, SOME.FIRST.NAME 2] SOME.ADDRESS 2

How do I remove anything thats found within, and including, the square brackets [] to create a cell that looks like the following?

SOME ADDRESS 1; SOME ADDRESS 2

You can use gsub with \\\\[.*?\\\\] to remove anything that's found within, and including, the square brackets [] :

x <- "[SOME.LAST.NAME, SOME.FIRST.NAME 1] SOME.ADDRESS 1; [SOME.LAST.NAME, SOME.FIRST.NAME 2] SOME.ADDRESS 2"
gsub("\\[.*?\\]", "", x)
#[1] " SOME.ADDRESS 1;  SOME.ADDRESS 2"

In case you also want to remove some spaces use:

trimws(gsub(" *\\[.*?\\]", "", x))
#[1] "SOME.ADDRESS 1; SOME.ADDRESS 2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM