[R] how to add comma to string vector?

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Fri Jun 10 12:19:57 CEST 2022

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

More information about the R-help mailing list