I have a couple hundred institution names that I would like to reformat. Currently they are in a vector, with just the college name in the each observation.
Is there a way to output this so that each college has text quotes around each name (so that it appears like "State University"), and then separate each observation by a comma? I tried using the paste() command, but the quotation marks seemed to make it so that I couldn't quote the punctuation attended.
Desired plain text output using write() function would be something like:
"State University", "Private University", "State Rival"
Replicable example:
state <- c("State University", "Private University", "Rival State")
CodePudding user response:
You can nest paste()
to get what you want:
paste(paste("\"", state, "\"", sep = ""), collapse = ",")
The inner paste()
quotes each element in your character vector, then the outer paste()
joins / collapses the vector into one string.
If you have the package stringr
installed, you can do it in one call:
library(stringr)
str_c("\"", state, "\"", collapse = ",")
CodePudding user response:
Using sprintf
and toString
to construct the string and writeLines
to write the output to text file.
writeLines(toString(sprintf('"%s"', state)), "temp.txt")