Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print vector name with vector value iteratively in R

I’m sure there’s already a solution to this question on here, but for the life of me, I can’t figure out the right way to search for it, so here goes.

Say I’ve got a named vector
vector <- c(a = 1, b = 2, c = 3)

How can I get the following output printed as a string, say using paste

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"a=1; b=2; c=3"

>Solution :

You can use paste like so:

paste(names(vector), "=", vector, collapse = "; ")
#[1] "a = 1; b = 2; c = 3"

Or use paste0 if you don’t want the space:

paste0(names(vector), "=", vector, collapse = "; ")
#[1] "a=1; b=2; c=3"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading