gpt4 book ai didi

r - 如何随机 gsub 文本

转载 作者:行者123 更新时间:2023-12-04 09:10:21 25 4
gpt4 key购买 nike

我正在尝试创建一些文本。我有一个数据框如下(里面有更多的文本行)

out<-data.frame(c("The road it long. It has many a winding path","The rain clouds are coming","My aunt has blue hair. It is like Marj Simpson's hair","The oesophagus is long. It is like a toothpaste tube"))

我想替换“它”。使用“和”,但我想随机执行此操作,以便示例输出为:

The road it long and has many a winding path
The rain clouds are coming
My aunt has blue hair. It is like Marj Simpson's hair
The oesophagus is long and is like a toothpaste tube

如何将随机替换的思想引入 gsubstr_replace

最佳答案

我们可以获得可能值的样本来替换,然后进行替换

v1 <- sample(c("and", "It"), nrow(out), replace = TRUE)
gsub("\\.\\s*(?=[a-z])", " ", str_replace_all(out[,1], "(?i)It", v1), perl = TRUE)
#[1] "The road and long and has many a winding path"
#[2] "The rain clouds are coming"
#[3] "My aunt has blue hair. It is like Marj Simpson's hair"
#[4] "The oesophagus is long and is like a toothpaste tube"

关于r - 如何随机 gsub 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com