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

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Feb 11 00:06:18 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
>
>   

# dummy example data
n = 3
x = replicate(10, paste(sample(letters, n), collapse=""))

y = lapply(1:n, function(i) substr(x, i, i))

# if you need a specific vector
x1 = y[[1]]

# if you really need all three as separate variables
for (i in 1:n)
    assign(paste('x', i, sep=""), y[[i]])

vQ



More information about the R-help mailing list