unlist(unpaste(a, ','))
accomplishes the split very well.
I'm not clear on what the unlist() is doing but everyone included it.
I tried both with and without the unlist() and didn't see a difference.
Also, in my case there was a leading blank to remove.
Some people suggested longer methods but
strip.blanks(unpaste(a,',')) worked great for me.
I find the S-Plus documentation frustrating.
Insightful's 7.0 'Function Guide' listed paste() under character
operations but not unpaste()?!
Thanx for the help:
Yuelin Li, Joe Davis, Richard Pugh, Francisco Javier Aguirre Navarro,
Patrick Burns, Sam Buttrey, James Holtman
Max Zhao wrote:
Thom,
Have you got any response yet? I had the same question before, and I had
to use "brute force" method. I discretize the string, and find the position
of the ",", then use string and position of "," to get the element. I
think the difficult question is find the position of ",". If that can be
done quickly, then it is easy to use substring to get the words separated by
"," without using the loop.
a <- "AB, ABC, FG, HIJKL, M"
##############
pos_c(3,8,12,19)
posL_c(1,pos+2) ### Assuming the first is not ","
posR_c(pos-1,nchar(a))
b_substring(a,posL,posR)
b ### This will be the one you want
[1] "AB" "ABC" "FG" "HIJKL" "M"
Regards,
Max
----- Original Message -----
From: "Thom Burnett" <Thom.Burnett@cognigencorp.com>
To: "S-News" <s-news@lists.biostat.wustl.edu>
Sent: Tuesday, May 24, 2005 11:00 AM
Subject: [S] Splitting a string into a vector of strings
What's the quick and easy way to make
a <- "AB, ABC, FG, HIJKL, M"
into
b <- c("AB", "ABC", "FG", "HIJKL", "M")
I haven't yet seen this at all using match , scan, regexpr, etc.
I know that I can locate the ',' and then loop to call substring but
that seems unecessarily complicated.
Thom
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|