如何在r中重新排序和替换字符串的部分?

时间:2021-05-02 18:51:43

I am trying to translate some textual information into R scripts. For that I need to substitute and reorder parts of the strings.

我正在尝试把一些文本信息翻译成R脚本。为此,我需要替换和重新排列字符串的部分。

example <- "varA is 1 and not varB is 1"

This is what I want as a result (a part of an R script):

这就是我想要的结果(R脚本的一部分):

exampleTrans <- "varA == 1 & varB != 1"

This is what I can do now:

这就是我现在能做的:

exampleTrans <- gsub(" is "," == ", example)
exampleTrans <- gsub(" and ", " & ", exampleTrans)
print(exampleTrans)
[1] "varA == 1 & not varB == 1"

The first part of the string is exactly what I wanted, so now I only need to change somthing in the second part. "not varB == 1" needs to be changed into "varB != 1".

弦的第一部分正是我想要的,所以现在我只需要在第二部分改变一些东西。“not varB == 1”需要更改为“varB != 1”。

Does anyone have an idea about how to do that? Is it even possible? Many thanks in advance!

有人知道怎么做吗?它甚至有可能吗?提前感谢!

2 个解决方案

#1


3  

Here's my solution using stringr:

这是我使用stringr的解决方案:

library(stringr)
str_replace_all(exampleTrans, "not (\\w+) =", "\\1 !")
[1] "varA == 1 & varB != 1"

Explanation: replace a pattern not (word) = with (word) !, where word is a variable name without spaces. Adjust it accordingly if you have specific variable names, containing e.g. digits or underscores.

说明:将模式not (word) =替换为(word) !,其中word是一个没有空格的变量名。如果您有特定的变量名,例如包含数字或下划线,请相应地调整它。

#2


0  

Ok, here is my solution :

好的,我的解决办法是:

  • First you need to split the string in two parts using str_split(). This is useful to detect the part of the string where you have the not.
  • 首先,需要使用str_split()将字符串分成两部分。这对于检测字符串中有not的部分很有用。
  • then you substitute is with == when not isn't there and with != when not is there.
  • 然后代入is = when not is not there, with != when not is there。
  • Then you can collapse the result with &.
  • 然后可以使用&折叠结果。

Here is my code :

这是我的代码:

library("stringr")
example <- "varA is 1 and not varB is 1"
out  <- str_split(example, "and")[[1]] 
ifelse(grepl(pattern = "not", x = out), sub(pattern = "([[:alpha:]]+) is ([[:digit:]]+)", replacement = "\\1 != \\2", x = out), 
    sub(pattern = "([[:alpha:]]+) is ([[:digit:]]+)", replacement = "\\1 == \\2", x = out)
   )
paste(out, collapse = "&")

Hope it works !

希望它能够工作!

#1


3  

Here's my solution using stringr:

这是我使用stringr的解决方案:

library(stringr)
str_replace_all(exampleTrans, "not (\\w+) =", "\\1 !")
[1] "varA == 1 & varB != 1"

Explanation: replace a pattern not (word) = with (word) !, where word is a variable name without spaces. Adjust it accordingly if you have specific variable names, containing e.g. digits or underscores.

说明:将模式not (word) =替换为(word) !,其中word是一个没有空格的变量名。如果您有特定的变量名,例如包含数字或下划线,请相应地调整它。

#2


0  

Ok, here is my solution :

好的,我的解决办法是:

  • First you need to split the string in two parts using str_split(). This is useful to detect the part of the string where you have the not.
  • 首先,需要使用str_split()将字符串分成两部分。这对于检测字符串中有not的部分很有用。
  • then you substitute is with == when not isn't there and with != when not is there.
  • 然后代入is = when not is not there, with != when not is there。
  • Then you can collapse the result with &.
  • 然后可以使用&折叠结果。

Here is my code :

这是我的代码:

library("stringr")
example <- "varA is 1 and not varB is 1"
out  <- str_split(example, "and")[[1]] 
ifelse(grepl(pattern = "not", x = out), sub(pattern = "([[:alpha:]]+) is ([[:digit:]]+)", replacement = "\\1 != \\2", x = out), 
    sub(pattern = "([[:alpha:]]+) is ([[:digit:]]+)", replacement = "\\1 == \\2", x = out)
   )
paste(out, collapse = "&")

Hope it works !

希望它能够工作!