0

What is the best way to read this type of character vector?

"global warming" OR "carbon sink" OR biodiversity OR conservation OR "global change"

and transform it to

"global+warming"+OR+"carbon+sink"+OR+biodiversity+OR+conservation+OR+"global+change"

jmutua
  • 290
  • 1
  • 12

3 Answers3

0

Is that what you want ?

text <- c("global warming" , "OR" , "carbon sink" , "OR" , "biodiversity" , "OR" , "conservation" , "OR" , "global change")

gsub(" " , "+" , do.call(paste , as.list(text)))

Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19
  • Nice try, you missed the quotation marks inside the output text `global+warming"+OR+"carbon+sink"+OR+biodiversity+OR+conservation+OR+"global+change"` – jmutua Jun 14 '22 at 09:51
  • What is the character vector you have ? – Mohamed Desouky Jun 14 '22 at 09:58
  • Here is a subset `"climate change" OR "climate crisis" OR "global warming" OR "carbon sink" OR biodiversity OR conservation OR "global change"` and I need an output like this `"climate+change"+OR+"climate+crisis"+OR+"global+warming"+OR+"carbon+sink"+OR+biodiversity+OR+conservation+OR+"global+change"` – jmutua Jun 14 '22 at 10:03
0

Assuming your string is read from a file /tmp/test you could try something like this:

st = readLines('/tmp/test')
gsub('\\s+', '+', gsub('\\\\|\\"', '', st))
andschar
  • 3,504
  • 2
  • 27
  • 35
0

Do you want something like:

s <- '"global warming" OR "carbon sink" OR biodiversity OR conservation OR "global change"'
cat(gsub(" ", "+", s))
#"global+warming"+OR+"carbon+sink"+OR+biodiversity+OR+conservation+OR+"global+change"

Without ".

cat(gsub('"', "", gsub(" ", "+", s)))
#global+warming+OR+carbon+sink+OR+biodiversity+OR+conservation+OR+global+change
GKi
  • 37,245
  • 2
  • 26
  • 48
  • Yes, can you capture the printed output without the back slashes and first and last `"`? – jmutua Jun 14 '22 at 11:10
  • `x <- gsub(" ", "+", s)` will capture it in `x`. You don't what it to start and end with `"`? – GKi Jun 14 '22 at 11:13
  • Yes, remove the first and the last `"`, as well as all `\\` – jmutua Jun 14 '22 at 11:17
  • Is the result of `gsub('"', "", gsub(" ", "+", s))` what you want? – GKi Jun 14 '22 at 11:25
  • No, I need the output from `gsub(" ", "+", s)` but without backslashes – jmutua Jun 14 '22 at 11:39
  • The backslashes are only shown on the terminal, escaping the `"` inside a character but they are not there. `cat` gives the string as it "really" is. – GKi Jun 14 '22 at 11:42
  • If I assign `gsub(" ", "+", s)` to a variable, the output is different from `cat(gsub(" ", "+", s))` – jmutua Jun 14 '22 at 11:51
  • Yes. The output on the console is what will be needed to create this string, escaping all `"` inside the string. `"a\"b"` is in fact `a"b` and `cat` is showing it as it is, the console is showing `"` escaped as `\"`. – GKi Jun 14 '22 at 11:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245592/discussion-between-jmutua-and-gki). – jmutua Jun 14 '22 at 12:08
  • See: [How do I deal with special characters like \^$.?*|+()[{ in my regex?](https://stackoverflow.com/q/27721008/10488504) – GKi Jun 14 '22 at 12:18