【发布时间】:2022-01-22 07:19:27
【问题描述】:
我有一些数据,如下所示:
df <-
data.frame(
'col' = c(
'some words [remove this] more words',
'some other words [I want this gone] this is fine',
'[nope. get rid of it] but keep this',
'all of this is fine',
'[but] this [should] go [away]')
)
col
1 some words [remove this] more words
2 some other words [I want this gone] this is fine
3 [nope get rid of it] but keep this
4 all of this is fine
5 [but] this [should] go [away]
我想删除所有方括号以及它们之间的所有内容。
goal_df <- df <-
data.frame(
'col' = c(
'some words more words',
'some other words this is fine',
'but keep this',
'all of this is fine',
'this go')
)
col
1 some words more words
2 some other words this is fine
3 but keep this
4 all of this is fine
5 this go
我认为使用正则表达式(这是我最差的编程技能)会是解决方案,但我似乎无法让它发挥作用。我正在使用df$col <- gsub( "[.*?]", "", df$col),但这并没有做任何改变。
【问题讨论】: