How to put quotes around several words quickly in Rstudio?

ε祈祈猫儿з 提交于 2020-08-22 12:10:09

问题


How to I make MI, ID, FL, into "MI", "ID", "FL" without having to type every single double quote?


回答1:


Use the following steps in RStudio:

  1. Select the text that you want each word quoted for.
  2. Open find/replace (command-f or ctrl-f)
  3. Check the "In selection" box
  4. Type comma space into the find field: [, ]
  5. Type quote comma space quote into the replace field: [", "]
  6. Select the All button after the replace field
  7. Type the initial and final quotation marks.

I use this procedure frequently. I hope someone will write an RStudio addin that provides a keyboard shortcut!




回答2:


You may want to check out the datapasta package!

If you follow directions under "Pasting a list as a horizontal vector with vector_paste()" of the linked vignette, you'll be able to type something like MI, ID, FL cut it and then use an assigned keyboard shortcut, say shift + cmd + v, to paste it into c("MI", "ID", "FL").




回答3:


The Hmisc package has a function Cs(), it will evaluate comma separated text as having quotes.

Cs(MI, ID, FL)
#becomes:
"MI", "ID", "FL"



回答4:


Assuming that you want this in order to make a vector, perhaps this function could be useful to you?

getSplit = function(oneString, delim){ return(unlist(strsplit(oneString, delim),use.names = F)) }

Example

oneStringInst = "MI, ID, FL" testResult = getSplit(oneStringInst, delim=", ")




回答5:


You can use paste for this:

> x <- c("MI", "ID", "FL")
> x
[1] "MI" "ID" "FL"
> paste0("\"", x, "\"")
[1] "\"MI\"" "\"ID\"" "\"FL\""

Note that R is showing the quotes themselves as being escaped, because it already puts double quotes around strings.



来源:https://stackoverflow.com/questions/38047593/how-to-put-quotes-around-several-words-quickly-in-rstudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!