Skip to content
Prev 205624 / 398506 Next

vectors into a matrix

On Jan 7, 2010, at 8:03 AM, varenda44 at gmail.com wrote:

            
library(fortunes)

 > fortune("parse")

If the answer is parse() you should usually rethink the question.
    -- Thomas Lumley
       R-help (February 2005)



An easier way is to use get() along with ls(), using a regex pattern  
in the latter. That will get you the objects which you can then  
manipulate as desired.


# see ?regex
 > ls(pattern = "^VD[0-9]+$")
[1] "VD1" "VD2" "VD3" "VD4"


# This presumes that each vector is the same length, such that using  
sapply() will return a matrix rather than a list.
 > t(sapply(ls(pattern = "^VD[0-9]+$"), get))
     [,1] [,2] [,3] [,4] [,5] [,6]
VD1   12   34   45    7   67   45
VD2   23   12   45   67   89   90
VD3   14   11   10   19   20   27
VD4   16   22   23   29   27   28

HTH,

Marc Schwartz