[R] how to add comma to string vector?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Jun 10 14:30:58 CEST 2022


Hello,

cat has a sep argument:

capture.output(cat(raws, sep = ","))
#[1] "field_1,field_2,field_3"


Instead of capture.output there is also cat argument file.

Hope this helps,

Rui Barradas


Às 11:19 de 10/06/2022, Luigi Marongiu escreveu:
> Hello,
> I need to convert an R vector to a python array. Essentially, I got a
> vector of strings where each element must be enclosed in single
> quotes. The problem is that each closing single quote should contain a
> comma. What is the regex trick to do that?
> I tried with:
> ```
>> (raws = c("field_1", "field_2",  "field_3"))
> [1] "field_1" "field_2" "field_3"
>> (items = sQuote(raws))
> [1] "'field_1'" "'field_2'" "'field_3'"
>> gsub("\'[:blank:]", "\',[:blank:]", items,  ignore.case = FALSE, perl = FALSE)
> [1] "'field_1'" "'field_2'" "'field_3'"
>> cat("python_Array = [", domain_list, "]\n")
> python_Array = [ 'Index' 'Endpoints' 'Confounders' 'Arboexposure' ]
> ```
> To note that `python_Array` does not have commas between elements and
> `gsub` did not do anything...
> 
> Thank you
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list