[R] Last char of a string in a list -> list of strings

Fredrik Karlsson dargosch at gmail.com
Wed Mar 18 18:07:41 CET 2009


Dear Gabor,

Thank you very much for this very helpful suggestion. Indeed, I was
making it too complicated by spliting the string into a vector.

Thank you!

/Fredrik

On Wed, Mar 18, 2009 at 3:37 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try this:
>
>> L <- c("!H*L", "L%", "%L", "H*L", "L%", "%L", "H*L", "H*L", "L%",
> + "%L", "H*L", "!H*L", "L%", "%L", "%L", "%L", "%L", "H*L", "!H*L",
> + "L%", "H*", "%L", "H*L", "L_%", "%L", "%H", "%H", "!H*L", "%H",
> + "H*", "%H", "%H", "H%", "H*", "!H*L", "H*L", "!H*L", "H*", "H*L",
> + "L_%", "%L", "%L", "H*L", "L*H", "H%", "%H", "L*H", "H%", "%H",
> + "H*L", "%H", "%L", "H*L", "%H", "%L", "H*L", "L%", "%L", "H*L",
> + "!H*L", "L%", "%L", "H*L", "%L", "H*L", "%H", "H*L", "%L")
>>
>> sub(".*([^*_!%0])[*_!%0]*$", "\\1", x)
>  [1] "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L"
> "L" "L" "L" "L" "H" "L" "L" "L" "L" "H" "H" "L" "H" "H" "H" "H" "H"
> "H" "L" "L" "L" "H"
> [39] "L" "L" "L" "L" "L" "H" "H" "H" "H" "H" "H" "L" "H" "L" "L" "H"
> "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "L" "H" "L" "L"
>
>
> On Wed, Mar 18, 2009 at 11:10 AM, Fredrik Karlsson <dargosch at gmail.com> wrote:
>> Dear list,
>>
>> This seems like a very simple problem, but I am failing to loose a
>> dimension (I think).
>> I have list, like this:
>>
>> ...
>> [2072] "!H*L" "L%"   "%L"   "H*L"  "L%"   "%L"   "H*L"  "H*L"  "L%"
>> "%L"   "H*L"  "!H*L" "L%"   "%L"   "%L"   "%L"   "%L"   "H*L"  "!H*L"
>> [2091] "L%"   "H*"   "%L"   "H*L"  "L_%"  "%L"   "%H"   "%H"   "!H*L"
>> "%H"   "H*"   "%H"   "%H"   "H%"   "H*"   "!H*L" "H*L"  "!H*L" "H*"
>> [2110] "H*L"  "L_%"  "%L"   "%L"   "H*L"  "L*H"  "H%"   "%H"   "L*H"
>> "H%"   "%H"   "H*L"  "%H"   "%L"   "H*L"  "%H"   "%L"   "H*L"  "L%"
>> [2129] "%L"   "H*L"  "!H*L" "L%"   "%L"   "H*L"  "%L"   "H*L"  "%H"
>> "H*L"  "%L"
>>
>> What I would like to do is
>>
>> 1) Remove all the %!*0 characters
>> 2) Get the last character of what remains for each string
>> 3) Get a list, comparable to the one above, but with only one
>> character (so, the last three data points would become list(H,L,L)
>>
>> I tried
>>
>>> strsplit(gsub("[*_!%0]*","",TADescSub$ExtendedLabel,""),"")
>>
>> but that gives me something like:
>>
>> ...
>> [[2136]]
>> [1] "H" "L"
>>
>> [[2137]]
>> [1] "H"
>>
>> [[2138]]
>> [1] "H" "L"
>>
>> [[2139]]
>> [1] "L"
>>
>> So, what am I doing wrong?
>>
>> /Fredrik
>>
>> --
>> "Life is like a trumpet - if you don't put anything into it, you don't
>> get anything out of it."
>>
>> ______________________________________________
>> 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.
>>
>



-- 
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."




More information about the R-help mailing list