R语言 将 *txt文件中的指定字符/字符串更改为双倍空格

yeotifhr  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(144)

我想在 *txt文件中将一个指定的字符/字符串改为双空格。在我的例子中,每个“x”都必须是双空格。
在我的例子中,我有:

my_text <- readLines("https://raw.githubusercontent.com/Leprechault/trash/main/test.txt")
my_text
 [1] "\"x\""                                                        
 [2] "\"Relatòrio de desempenho dos diferentes produtos testados\"" 
 [3] "\"x\""                                                        
 [4] "\"Vittia P&D\""                                               
 [5] "\"x\""                                                        
 [6] "\"Data:2022-12-27\""                                          
 [7] "\"x\""                                                        
 [8] "\"Período de tempo analisado\""                               
 [9] "\"x\""                                                        
[10] "24"                                                           
[11] "\"Df\" \"Deviance\" \"Resid. Df\" \"Resid. Dev\" \"Pr(>Chi)\""

我的理想输出必须是:

my_text_new <- readLines("test_new.txt")
my_text_new
 [1] ""                                                             
 [2] ""                                                             
 [3] "\"Relatòrio de desempenho dos diferentes produtos testados\"" 
 [4] ""                                                             
 [5] ""                                                                                                                         
 [6] "\"Vittia P&D\""                                               
 [7] ""                                                             
 [8] ""                                                                                                                         
 [9] "\"Data:2022-12-27\""                                          
[10] ""                                                             
[11] ""                                                                                                                        
[12] "\"Período de tempo analisado\""                               
[13] ""                                                             
[14] ""                                                                                                                       
[15] "24"                                                           
[16] ""                                                             
[17] ""                                                             
[18] "\"Df\" \"Deviance\" \"Resid. Df\" \"Resid. Dev\" \"Pr(>Chi)\""

拜托,能帮上忙吗?
提前感谢!

uqdfh47h

uqdfh47h1#

my_text <- readLines("https://raw.githubusercontent.com/Leprechault/trash/main/test.txt")

my_text <- my_text[rep(seq(my_text), (as.integer(my_text == "\"x\"")+1))]
my_text[which(my_text == "\"x\"")] <- ""

head(my_text)
#> [1] ""                                                               
#> [2] ""                                                               
#> [3] "\"Relat\xf2rio de desempenho dos diferentes produtos testados\""
#> [4] ""                                                               
#> [5] ""                                                               
#> [6] "\"Vittia P&D\""

请原谅我的unicode转义我没有葡萄牙语配置在这台电脑

相关问题