[R] help to slip a file name using "strsplit" function

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jan 25 19:10:00 CET 2012


On Wed, Jan 25, 2012 at 10:51 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Wed, Jan 25, 2012 at 10:26 AM, gianni lavaredo
> <gianni.lavaredo at gmail.com> wrote:
>> Dear Researchers,
>>
>> I have several files as this example: Myfile_MyArea1_sample1.txt
>>
>> i wish to split in "Myfile", "MyArea1", "sample1", and "txt", becasue i
>> need to use "sample1" label. I try to use "strsplit" but I am able just to
>> split as "Myfile_MyArea1_sample1" and "txt" OR "Myfile", "MyArea1",
>> "sample1.txt" using
>>
>>
>> strsplit(as.character("Myfile_MyArea1_sample1.txt"), ".", fixed = TRUE)
>> strsplit(as.character("Myfile_MyArea1_sample1.txt"), "_", fixed = TRUE)
>>
>
> Replace the . with _ and then strsplit on _ like this:
>
>> strsplit(chartr(".", "_", "Myfile_MyArea1_sample1.txt"), "_")[[1]]
> [1] "Myfile"  "MyArea1" "sample1" "txt"
>
> If the idea is just to extract "sample1" then try replacing everything
> up to the last _ and everything from the dot onwards with empty
> strings like this:
>
>> gsub(".*_|[.].*", "", "Myfile_MyArea1_sample1.txt")
> [1] "sample1"
>

Actually the strsplit above could be shortened to:

> strsplit("Myfile_MyArea1_sample1.txt", "[._]")[[1]]
[1] "Myfile"  "MyArea1" "sample1" "txt"

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list