3
\$\begingroup\$

An oddly specific task - I have a dataframe with columns with values I need to concatenate in order to prepare a query for a website that requires very specific inputs: unquoted strings, separated by commas. Here's a dput() of a subset of the dataframe with the columns needed:

d = structure(list(CodeSpace = c(1303L, 1303L, 1303L, 1303L, 1303L, 
1303L), TagID = c(23041L, 23042L, 23044L, 23046L, 23043L, 23045L
)), class = "data.frame", row.names = c(NA, -6L))

The output I need is:

A69-1303-23041, A69-1303-23042, A69-1303-23044, A69-1303-23046, A69-1303-23043, A69-1303-23045

I can approximate it (though I end up with a trailing comma) with:

cat(paste0(noquote(paste("A69", d$CodeSpace, d$TagID, sep = "-")) , ","))

Is there a better (i.e. more principled) way to do this?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

A more conventional way to do that is to use the collapse argument of paste():

cat(paste(paste("A69", d$CodeSpace, d$TagID, sep = "-"), collapse = ", "))
# A69-1303-23041, A69-1303-23042, A69-1303-23044, A69-1303-23046, A69-1303-23043, A69-1303-23045
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.