[R] how to add comma to string vector?

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Fri Jun 10 12:48:26 CEST 2022


Have you considered saving it to a csv file in R and then reading the file in python?
Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Ivan Krylov
Sent: Friday, June 10, 2022 6:34 AM
To: Luigi Marongiu <marongiu.luigi using gmail.com>
Cc: r-help <r-help using r-project.org>
Subject: Re: [R] how to add comma to string vector?

[External Email]

On Fri, 10 Jun 2022 12:19:57 +0200
Luigi Marongiu <marongiu.luigi using gmail.com> wrote:

> I need to convert an R vector to a python array.

Have you considered using the reticulate package on the R side or the
rpy2 package on the Python side? It's hard to cover all edge cases when producing source code to be evaluated by another language. You may encounter a corner case where you output a character special to Python without properly escaping it and end up with an injection error (see
also: SQL injection vulnerability, the most widely known type of this mistake). For producing R code from R values, there's deparse(), and even that's not perfect:
https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.r-2Dproject.org_show-5Fbug.cgi-3Fid-3D18232&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=unYB3ajJ6LhkNKab1X8TGSNYaiYwhgEC7GZfknFfA9Q&e=

Having said that, paste(sQuote(values, FALSE), collapse = ',') will do the trick, but only if values are guaranteed not to contain single quotes or other characters that have special meaning in Python. Note the FALSE argument to sQuote: otherwise it could return ‘Unicode quotes’, `TeX quotes', or even «guillemets», depending on the options set by the user.

--
Best regards,
Ivan

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=10-fNd6yyMRCfduzKbeG6MHUz5Tkf4rf6lbKc7OFuSM&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=j_RwC0Njzj5WVBbGGmslVCm2EoDQxs7iNbMXItIRobM&e=
and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list