Print an R vector vertically

Try:

cat( paste( vector, collapse='\n' ) )

This essentially creates a string of vector elements and prints to console with newline character.


You can use stack(),

with(mtcars, stack(sapply(split(mpg, cyl), mean)))
#    values ind
#1 26.66364   4
#2 19.74286   6
#3 15.10000   8

But aggregate() would probably be much nicer for this problem

aggregate(mpg ~ cyl, mtcars, mean)
#  cyl      mpg
#1   4 26.66364
#2   6 19.74286
#3   8 15.10000

Also, tapply() might be a better fit than sapply(), if you go that route.

stack(with(mtcars, tapply(mpg, cyl, mean)))
#    values ind
#1 26.66364   4
#2 19.74286   6
#3 15.10000   8

With sapply(), you're going to get a vector if the return values are only on one row, so you may want to use a different function for the calculations.

Tags:

R