0

I have used paste and gsub before. I have a couple of years experience in R. However now I am not looking to simply merge strings into one. I need the strings to stay seperated and just divided by a comma:

one <- letters[1:9]
assigned.object <- somefunction(one,sep=",")
assigned.object
[1] "a","b","c","d","e","f","g","h","i"

Also searched the site and this thread and its duplicate don't answer my question. Could not find any other answers to this issue. Anybody knows a way in R?

ANSWER:

The duplicate answers the question. However if you need to assign an object to what you directly want this happens:

cat(paste("\"",as.character(one),"\"",collapse=", ",sep=""))
[1] "a","b","c","d","e","f","g","h","i"
assigned.object <- cat(paste("\"",as.character(one),"\"",collapse=", ",sep=""))
assigned.object
NULL

The reason is that there is somewhat an 'error in translation': R can print the object I want, but not store it like the this. The object I want needs to be stored like this:

assigned.object <- paste("\"",as.character(one),"\"",collapse=", ",sep="")
assigned.object
[1] "\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\""

which looks like this if you save it in a txt-file

"a","b","c","d","e","f","g","h","i"

Basically R needs to insert a '\' to distinguish between quotes in the character and those quotes which define beginning and ending in the character.

Community
  • 1
  • 1
5th
  • 2,097
  • 3
  • 22
  • 41
  • 2
    Like this? `paste(letters[1:9], collapse = ',')` – Gopala May 21 '16 at 15:36
  • no. This gives you only "a,b,c,d,e,f,g,h,i". Which won't help. – 5th May 22 '16 at 11:27
  • @5th, please have a look at the other answers. If you think your question is different or none of them provide the answer you are looking for, you can revise your question so we can reopen it. – Sotos May 22 '16 at 12:03
  • 1
    No don't. The duplicate answers actually the question. Just let me edit this answer to explain how. – 5th May 22 '16 at 12:19
  • ok actually just realised that while this procedure works well for sort of table-like file-types (e.g. .csv, .tsv), it does not work for .json-files (which I needed). – 5th May 24 '16 at 14:15

0 Answers0