1
$\begingroup$

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 <- c()
for( nms in name){ t <- 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.

$\endgroup$
5
  • $\begingroup$ I believe I addressed this in the comments on your other question (datascience.stackexchange.com/questions/9342/…)! If that adequately fixes your problem, can you close this one? Thanks! $\endgroup$ – Kyle. Dec 11 '15 at 22:08
  • $\begingroup$ Please see the comments. As I mentioned here, I couldn't get single backslash. paste function is giving me double backslashes like \\'name1\\', etc. Inside read.csv, double backslashes produces error. It only take single backslash. $\endgroup$ – user62198 Dec 11 '15 at 22:17
  • $\begingroup$ I am kind of in a time crunch. That's why I opened another question in the hope of reaching others who may not have seen this. No other reason. $\endgroup$ – user62198 Dec 11 '15 at 22:18
  • $\begingroup$ cat("\\'",name,"\\'") function is giving desired results, but let me try to fit this into your function $\endgroup$ – Jijo Dec 18 '15 at 7:11
  • $\begingroup$ try to write it as csv write.csv(t,"t.csv") it gives the desired result means that R internally stores it as expected $\endgroup$ – Jijo Dec 18 '15 at 9:22
2
$\begingroup$

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 <- c("word1","word2","word3")

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

#Combine the vector into the string
line <-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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.