vector_to_code <- function(vec, vec_name) {
if (is.character(vec)) {
paste0(vec_name, " <- c('", paste0(vec, collapse = "', '"), "')")
} else {
paste0(vec_name, " <- c(", paste0(vec, collapse = ", "), ")")
}
}
# example
a <- c(1, 2, 3)
vector_to_code(a, "a_new")
b <- c("a", "b", "c")
vector_to_code(b, "b_new")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter