On Wed, Jan 25, 2012 at 10:51 AM, Gabor Grothendieck
<ggrothendi...@gmail.com> wrote:
> On Wed, Jan 25, 2012 at 10:26 AM, gianni lavaredo
> <gianni.lavar...@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

______________________________________________
R-help@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.

Reply via email to