Dear all, I have following kind of character vector: Vec <- c("344426", "dwjjsgcj", "123sgdc", "aagha123", "sdh343asgh", "123jhd51") Now I want to split each element of this vector according to?numeric?and string element. For example in the 1st element of that vector, there is no string element. Therefore I should get a vector of length 2 like c("", "344426") and so on. Can somebody point me how to achieve that in R? Is there any specific function for doing that? Thanks,
On Sun, Jun 26, 2011 at 10:54 AM, Megh Dal <megh700004 at yahoo.com> wrote:> Dear all, I have following kind of character vector: > > Vec <- c("344426", "dwjjsgcj", "123sgdc", "aagha123", "sdh343asgh", "123jhd51") > > > Now I want to split each element of this vector according to?numeric?and string element. For example in the 1st element of that vector, there is no string element. Therefore I should get a vector of length 2 like c("", "344426") and so on. > > Can somebody point me how to achieve that in R? Is there any specific function for doing that? >Try this and see the gsubfn home page at http://gsubfn.googlecode.com for more info: library(gsubfn) strapply(Vec, "\\d+|\\D+", c) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Jun 26, 2011, at 10:54 AM, Megh Dal wrote:> Dear all, I have following kind of character vector: > > Vec <- c("344426", "dwjjsgcj", "123sgdc", "aagha123", "sdh343asgh", > "123jhd51") > > > Now I want to split each element of this vector according to numeric > and string element. For example in the 1st element of that vector, > there is no string element. Therefore I should get a vector of > length 2 like c("", "344426") and so on. > > Can somebody point me how to achieve that in R? Is there any > specific function for doing that??regex ?strsplit You don't do a very good job of describing your desired output, so this is two versions of what I am guessing that to be: > cbind(lapply(strsplit(Vec, "[^0-9]+"), paste, collapse=""), + lapply(strsplit(Vec, "[0-9]+"), paste, collapse="") ) [,1] [,2] [1,] "344426" "" [2,] "" "dwjjsgcj" [3,] "123" "sgdc" [4,] "123" "aagha" [5,] "343" "sdhasgh" [6,] "12351" "jhd" > data.frame(numbits=unlist(lapply(strsplit(Vec, "[^0-9]+"), paste, collapse="")), + alphabits=unlist(lapply(strsplit(Vec, "[0-9]+"), paste, collapse="")) ) numbits alphabits 1 344426 2 dwjjsgcj 3 123 sgdc 4 123 aagha 5 343 sdhasgh 6 12351 jhd -- David Winsemius, MD West Hartford, CT