Re: [R] convert a vector of words into a matrix

From: Wacek Kusnierczyk <>
Date: Thu, 24 Jul 2008 11:51:51 +0200

Daren Tan wrote:
> I want the matrix to look like this:
> [,1] [,2] [,3] [,4]
> [1,] "1" "2" "3"
> [2,] "1" "2" [3,] "1" "2" "4" "5"
> I tried to use, strings) but failed due to unequal row lengths.
you can't {r,c}bind them because of different lengths. one way to reach the goal is to pad all vectors with some dummy value (NA, say), and then bind them.

one way (assuming strings is the list you get out of strsplit):

lengths = sapply(strings, length)
ncol = max(lengths)



       function(vector, length)
            c(vector, rep(NA, ncol-length)),

there may be a better way, though.

vQ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 24 Jul 2008 - 09:54:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jul 2008 - 11:32:37 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive