问题
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:
- Select the text that you want each word quoted for.
- Open find/replace (command-f or ctrl-f)
- Check the "In selection" box
- Type comma space into the find field: [, ]
- Type quote comma space quote into the replace field: [", "]
- Select the All button after the replace field
- 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