[R] Turning a string into a real vector

tony at shatalmic.com tony at shatalmic.com
Mon Sep 23 18:49:53 CEST 2013


I have the following string input:

input <-
"ACC,1.1,2.2,3.3,4.4\nACC,2.2,3,4,5\nADN,3.3,4,5\nACC,4.4,5.5,6.6,7.7\nADN,5.5,6,7\n"

Note that \n is a real line feed in the data and the numbers are all made up
and might be other values. The key is that the first number is a float, the
ACC packets are all floats and the ADN the first value is a float and the
rest are ints.

I want to turn this string into a vector of packets. I tired using grep and
regexpr without luck. I would also like to filter the packets so that I can
get a vector of just the ACC packets and another vector of just the ADN
packets.

When I tried this:

grep ("ACC", input, FALSE, FALSE, TRUE)

What I ended up with was:

[1] "ACC,1.1,2.2,3.3,4.4"            "ACC,2.2,3,4,5"         "ADN,3.3,4,5"                   
"ACC,4.4,5.5,6.6,7.7"           "ADN,5.5,6,7"

What I wanted was:

[1] "ACC,1.1,2.2,3.3,4.4"
[2] "ACC,2.2,3,4,5"
[3] "ACC,4.4,5.5,6.6,7.7"

Then I wanted to put in grep ("ADN", input, FALSE, FALSE, TRUE) and get out:

[1] "ADN,3.3,4,5"
[2] "ADN,5.5,6,7"

Can someone help me figure this out?



--
View this message in context: http://r.789695.n4.nabble.com/Turning-a-string-into-a-real-vector-tp4676751.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list