[R] Splitting a vector

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Dec 10 09:02:39 CET 2013


You don't need to wrap 1:12 in c().

 Since matrices are just folded vectors, you can convert vector X to a matrix Xm:

Xm <- matrix( X, nrow=3 )

and access columns to get your your sub-vectors:

Xm[,1]
Xm[,2]

and so on.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Long Vo <long_vo_hai at yahoo.com.vn> wrote:
>Hi, I am quite new to R so I know that this probably is very basic ,
>but how
>can I split a sequence of number into multiple parts with equal length?
>For example I have a vector
>
>X=c(1:12)
>I simply need to split it into sub-vectors with the same length N . Say
>N=3
>then I need the output to be like 
>1 2 3
>4 5 6
>7 8 9
>10 11 12
>
>And better if the sub-vectors can be named so that I can use them later
>for
>individual study, probably a do-loop in which a function can be applied
>to
>them.
>I just want them to be in consecutive order so really no fancy
>conditions
>here.  
>
>Any helps to this amateur is greatly appreciated,
>Long
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Splitting-a-vector-tp4681930.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list