gianni lavaredo
2012-Jan-25 15:26 UTC
[R] help to slip a file name using "strsplit" function
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)
Thanks in advance
Gianni
[[alternative HTML version deleted]]
Ivan Calandra
2012-Jan-25 15:40 UTC
[R] Fwd: help to slip a file name using "strsplit" function
Hi Gianni,
Your first example works fine, though you have unnecessary stuff in it:
strsplit("Myfile_MyArea1_sample1.txt", "_")
[[1]]
[1] "Myfile" "MyArea1" "sample1.txt"
The second example doesn't work because of two things: the period is a
special character, and the fixed argument should be false:
strsplit("Myfile_MyArea1_sample1.txt", "\\.")
[[1]]
[1] "Myfile_MyArea1_sample1" "txt"
HTH,
Ivan
-------- Message original --------
Sujet: [R] help to slip a file name using "strsplit" function
Date : Wed, 25 Jan 2012 16:26:11 +0100
De : gianni lavaredo <gianni.lavaredo at gmail.com>
Pour : r-help at r-project.org
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)
Thanks in advance
Gianni
[[alternative HTML version deleted]]
______________________________________________
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.
--
Ivan CALANDRA
Universit? de Bourgogne
UMR CNRS/uB 6282 Biog?osciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calandra at u-bourgogne.fr
Gabor Grothendieck
2012-Jan-25 15:51 UTC
[R] help to slip a file name using "strsplit" function
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" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
How about:
unlist(strsplit(as.character("Myfile_MyArea1_sample1.txt"), split =
'[[:punct:]]'))
-----Original Message-----
From: gianni lavaredo
Sent: Wednesday, January 25, 2012 9:26 AM
To: r-help at r-project.org
Subject: [R] help to slip a file name using "strsplit" function
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)
Thanks in advance
Gianni
[[alternative HTML version deleted]]
______________________________________________
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.
------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965
Gabor Grothendieck
2012-Jan-25 18:10 UTC
[R] help to slip a file name using "strsplit" function
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