Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环替换非结构化文本文件中的单词_R_Loops_For Loop - Fatal编程技术网

使用for循环替换非结构化文本文件中的单词

使用for循环替换非结构化文本文件中的单词,r,loops,for-loop,R,Loops,For Loop,我有一个非常非结构化的文本文件,我用readLines阅读。我想将某些字符串更改为变量中的另一个字符串(下面称为“new”) 下面我希望操纵的文本包含所有术语:“一”、“二”、“三”和“四”一次,而不是“更改”字符串。但是,正如您所看到的,sub更改了每个元素中的第一个模式,但是我需要代码忽略带引号的新字符串 请参见下面的示例代码和数据 #text to be changed text <- c("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT

我有一个非常非结构化的文本文件,我用readLines阅读。我想将某些字符串更改为变量中的另一个字符串(下面称为“new”)

下面我希望操纵的文本包含所有术语:“一”、“二”、“三”和“四”一次,而不是“更改”字符串。但是,正如您所看到的,sub更改了每个元素中的第一个模式,但是我需要代码忽略带引号的新字符串

请参见下面的示例代码和数据

 #text to be changed
 text <- c("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT change",
        "TEXT TEXT TEXT change TEXT TEXT TEXT TEXT TEXT change", 
        "TEXT TEXT TEXT change TEXT TEXT TEXT TEXT")

 #Variable containing input for text
 new <- c("one", "two", "three", "four")
 #For loop that I want to include 
 for (i in 1:length(new)) {

   text  <- sub(pattern = "change", replace = new[i], x = text)

 }
 text
#要更改的文本

text这个怎么样?逻辑是,锤掉一个字符串,直到它不再有变化。在每次“点击”(找到
change
)时,沿着
new
向量移动

text <- c("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT change",
          "TEXT TEXT TEXT change TEXT TEXT TEXT TEXT TEXT change", 
          "TEXT TEXT TEXT change TEXT TEXT TEXT TEXT")

#Variable containing input for text
new <- c("one", "two", "three", "four")
new.i <- 1

for (i in 1:length(text)) {
  while (grepl(pattern = "change", text[i])) {
    text[i] <- sub(pattern = "change", replacement = new[new.i], x = text[i])
    new.i <- new.i + 1
  }
}
text

[1] "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT one" 
[2] "TEXT TEXT TEXT two TEXT TEXT TEXT TEXT TEXT three"
[3] "TEXT TEXT TEXT four TEXT TEXT TEXT TEXT" 

text下面是另一个使用
gregexpr()
regmatches()
的解决方案:

#要更改的文本

text另一种使用strsplit的方法:

tl <- lapply(text, function(s) strsplit(s, split = " ")[[1]])
df <- stack(setNames(tl, seq_along(tl)))

ix <- df$values == "change"
df[ix, "values"] <- new
tapply(df$values, df$ind, paste, collapse = " ")
此外,您还可以将
taply
调用包装成
unname

 unname(tapply(df$values, df$ind, paste, collapse = " "))
其中:


如果只想使用
new
的元素一次,可以将代码更新为:

newnew <- new[1:3]

ix <- df$values == "change"
df[ix, "values"][1:length(newnew)] <- newnew
unname(tapply(df$values, df$ind, paste, collapse = " "))

你需要文本吗?我喜欢这种方法。我看到的唯一一个警告是,在某些字符串中可能有
\n
,在这一点上,事情会在最后一步中击中众所周知的风扇,这就是为什么我没有使用串联的类似方法。
[1] "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT one" 
[2] "TEXT TEXT TEXT two TEXT TEXT TEXT TEXT TEXT three"
[3] "TEXT TEXT TEXT four TEXT TEXT TEXT TEXT"
newnew <- new[1:3]

ix <- df$values == "change"
df[ix, "values"][1:length(newnew)] <- newnew
unname(tapply(df$values, df$ind, paste, collapse = " "))
newnew2 <- c(new, "five")

tl <- lapply(text, function(s) strsplit(s, split = " ")[[1]])
df <- stack(setNames(tl, seq_along(tl)))

ix <- df$values == "change"
df[ix, "values"][1:pmin(sum(ix),length(newnew2))] <- newnew2[1:pmin(sum(ix),length(newnew2))]
unname(tapply(df$values, df$ind, paste, collapse = " "))