How to build the following the string in R? I am trying to build a long string of the form.....\'name1\',\'name2\',\'name3\',..etc. How do I do it ? I am trying to do it using R ? By hand is impossible as there are currently over 200 such names and in future I may have more. So I need to automate the process. However, every time I tried to use R's paste0 function I am getting a different result. Here is what I did:

t &lt;- c()
for( nms in name){ t &lt;- paste0(t, paste0("\\'",nms,"\\'", sep = ",") ) }


However, the result turns out to include double backslashes instead of one, like this "\\'name1\\',\\'name2\\',\\'name3\\' etc. I couldn't get a single backslash as I want. 

Any help would be appreciated.

Thanks.
The way R displays the string (using print command) is different from the actual value of this string. Even though you see 2 backslashes for your output, the actual string contains a single backslash. This is because when you type the name of your variable it shows the value within the quotes and within quotes it should use 2 backslashes. When you display the value of the sting using message() or other similar function, when it just prints the value without quotes, then you will see a single back slash only:

#create a vector of names (words)
words &lt;- c("word1","word2","word3")

#add prefix and suffix part to each word
words_vec &lt;- paste0("\\'",words,"\\'")
words_vec
"\\'word1\\'" "\\'word2\\'" "\\'word3\\'"

#Combine the vector into the string
line &lt;-paste(words_vec,collapse='')
line
"\\'word1\\'\\'word2\\'\\'word3\\'"

#Check how the string looks:
message(line)
\'word1\'\'word2\'\'word3\'


By the way, the above (vectorized) method is more efficient than the for-loop method you are using to put all names together.