Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a named vector in R?

Tags:

r

Suppose I have this named vector:

> foo = setNames(c("one", "two"), c(1, 2))
> foo
    1     2 
"one" "two" 
> names(foo)
[1] "1" "2"
> foo
    1     2 
"one" "two" 

What is the easiest way to print the following:

1: one, 2: two

I just want it for debugging.

Could be with or without quotes, I'm not picky.

I've got this, but it seems very chatty:

the_vec = c()
for (idx in 1:length(foo)) {
  the_vec = c(the_vec, paste(idx, ":", foo[idx], sep=""))
}
paste(the_vec, collapse=", ")

output:

[1] "1:one, 2:two"
like image 953
dfrankow Avatar asked Dec 15 '22 01:12

dfrankow


1 Answers

paste(names(foo), foo, sep = ":", collapse = ",")
like image 139
joel.wilson Avatar answered Jan 04 '23 07:01

joel.wilson