[R] How to split a character vector into 3 vectors

jdeisenberg catcode at catcode.com
Tue Feb 10 20:54:29 CET 2009



kayj wrote:
> 
> Hi ,
> 
> 
> Does any one know how to split a character vector , I have a vector X that
> looks like this and each row has 3 characters
> 
>   X
> ASK
> DGH
> ASG
> AUJ
> FRT
> 
> I would like to split the vector into 3 vectors that look like this
> 
> X1	X2	X3
> A  	S	K
> D	G	H
> A	S	G
> A	U	J
> U	R	T
> 
> thanks
> 
> 

If I understand you correctly, you have this vector:

x <- c("ASK", "DGH", "ASG", "AUJ", "URT")

This code seems to do what you want:

x1 <- substr(x, 1, 1)
x2 <- substr(x, 2, 2)
x3 <- substr(x, 3, 3)

There's probably a much simpler and more elegant way to do it, though.
-- 
View this message in context: http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939521.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list