[R] How to get a matrix by sapply (with strsplit)?

hadley wickham h.wickham at gmail.com
Fri Dec 4 06:15:24 CET 2009


> I'm not sure what the other lines(but the last) have to do anything, but are you looking for something like this:
>
> do.call(rbind, sapply(paste(1:10, 1:10), strsplit, split=' '))

strsplit is already vectorised wrt its first argument, so all you need is:

do.call(rbind, strsplit(paste(1:10, 1:10), split=' '))

Alternatively, you can use str_split_fixed from the stringr package:

library(stringr)
str_split_fixed(paste(1:10, 1:10), " ", 2)

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list