[R] String Manipulation- Extract numerical and alphanumerical segment

jim holtman jholtman at gmail.com
Fri Feb 5 16:29:12 CET 2010


Does this help:

> x <- c("1234567.z3.abcdef-gh.12","1234567.z3.abcdef-gh.12","1234567.z3.abcdef-gh.12")
> y <- strsplit(x, '[.]')
>
> y
[[1]]
[1] "1234567"   "z3"        "abcdef-gh" "12"

[[2]]
[1] "1234567"   "z3"        "abcdef-gh" "12"

[[3]]
[1] "1234567"   "z3"        "abcdef-gh" "12"

> y.1 <- sapply(y, '[[', 1)
> y.1
[1] "1234567" "1234567" "1234567"
> y.2 <- sapply(y, '[[', 2)
> y.2
[1] "z3" "z3" "z3"
>


On Fri, Feb 5, 2010 at 10:11 AM, Su C. <sushikyc at gmail.com> wrote:
>
> I am currently attempting to split a long list of strings (let's call it
> "string.list") that is of the format:
>
> "1234567.z3.abcdef-gh.12"
>
> I have gotten it to:
> "1234567"  "z3"  "abcdef-gh"  "12"
> by use of the strsplit function.
>
> This leaves me with each element of "string.list" having a split string of
> the above format. What I'd like to do now is extract the first two strings
> of each element in "string.list" -- the "1234567" and the "z3" -- and place
> them into two separate lists, say, "firstsplit.numeric.list" and
> "secondsplit.alphanumeric.list"
>
> I'm having some trouble figuring out how to do this. Any help would be
> greatly appreciated!
> --
> View this message in context: http://n4.nabble.com/String-Manipulation-Extract-numerical-and-alphanumerical-segment-tp1470301p1470301.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list