[R] Splitting a vector

arun smartpink111 at yahoo.com
Tue Dec 10 05:51:33 CET 2013


Hi,
Try:
split(X,as.numeric(gl(length(X),3,length(X))))
A.K.


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


More information about the R-help mailing list