简体   繁体   中英

Reading character vector in R

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"

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)))

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

st = readLines('/tmp/test')
gsub('\\s+', '+', gsub('\\\\|\\"', '', st))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM