r - How to convert object variable into string inside a function -


i have following list of vectors


v1 <-  c("foo","bar") v2 <-  c("qux","uip","lsi")  mylist <- list(v1,v2)  mylist #> [[1]] #> [1] "foo" "bar" #>  #> [[2]] #> [1] "qux" "uip" "lsi" 

what want apply function prints string:

v1:foo,bar v2:qux,uip,lsi 

so involves 2 step: 1) convert object variable string , 2) make vector string. latter easy can this:

make_string <- function (content_vector) {   cat(content_vector,sep=",")  }  make_string(mylist[[1]]) # foo,bar make_string(mylist[[2]]) # qux,uip,lsi 

i aware of this solution, don't know how can turn object name string within function prints desired output.

i need to inside function, because there many other output need process.

we can use

cat(paste(c('v1', 'v2'), sapply(mylist, tostring), sep=":", collapse="\n"), '\n') #v1:foo, bar #v2:qux, uip, lsi  

if need pass original object i.e. 'v1', 'v2'

make_string <- function(vec){     obj <- deparse(substitute(vec))      paste(obj, tostring(vec), sep=":") }   make_string(v1) #[1] "v1:foo, bar" 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -