如何在R中替换字符串中的单词?[已关闭]

lndjwyie  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(117)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

12天前关闭。
Improve this question
我有句话-“送礼时,[最好]”

I need to replace [Best] with {better; ideal; most effective} three words and return 3 sentences ,in a list.
"When giving a Gift, it is better"
"When giving a Gift, it is ideal"
"When giving a Gift, it is most effective"

我如何在R中实现它。

lnlaulya

lnlaulya1#

使用gsub

snt <-  "When giving a Gift, it is [Best] to"

alt <- list('better', 'ideal', 'moste effective')

lapply(alt, \(x) gsub('\\[Best\\]', x, sentence))
# [[1]]
# [1] "When giving a Gift, it is better to"
# 
# [[2]]
# [1] "When giving a Gift, it is ideal to"
# 
# [[3]]
# [1] "When giving a Gift, it is moste effective to"

相关问题