Hi All, I have a very simple problem. I have several files in a same directory. I would like to send for an object only the files that finish in ".sens.". I execute the command below, files <- dir(pattern="*.sens") but it includes all of the files that have "sens", independent of they be in the end or in the middle of the name of the file. How could I solve this? I sought in the html_help but I didn't find similar to this. My files "script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", "Sen_155_03_R3.sens", "tome2sens_time1sens.txt" Tahnks very much -- Marcelo Luiz de Laia, M.Sc. Dep. de Tecnologia, Lab. Bioqu?mica e de Biologia Molecular Universidade Estadual Paulista - UNESP Via de Acesso Prof. Paulo Donato Castelane, Km 05 14.884-900 - Jaboticabal, SP, Brazil PhoneFax: 16 3209-2675/2676/2677 R. 202/208/203 (trab.) HomePhone: 16 3203 2328 - www.lbm.fcav.unesp.br - mlaia at yahoo.com
Marcelo Luiz de Laia wrote:>[...] only the files that finish in ".sens.". I execute the command below, > files <- dir(pattern="*.sens")files <- list.files("./", "\.sens$")>-- Michael T. Mader Institute for Bioinformatics/MIPS, GSF Ingolstaedter Landstrasse 1 D-85764 Neuherberg 0049-89-3187-3576 response time (n.) An unbounded, random variable Tr associated with a given TIMESHARING system and representing the putative time which elapses between Ts, the time of sending a message, and Te, the time when the resulting error diagnostic is received. S. Kelly-Bootle, The Devil's DP Dictionary
On Thu, 29 Jan 2004 11:33:25 -0300, Marcelo Luiz de Laia <mlaia at fcav.unesp.br> wrote :>Hi All, > >I have a very simple problem. I have several files in a same directory. I would like to send for an object only the files that finish in ".sens.". I execute the command below, > >files <- dir(pattern="*.sens") > >but it includes all of the files that have "sens", independent of they be in the end or in the middle of the name of the file. How could I solve this? I sought in the html_help but I didn't find similar to this. > >My files > >"script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", "Sen_155_03_R3.sens", "tome2sens_time1sens.txt"The pattern in dir() is a regular expression pattern, not a filename wildcard. You want dir(pattern="\\.sens$"). (The double backslash comes because the regular expression you want contains a backslash before the dot.) If you're using Windows, the choose.files() interactive function is probably more friendly. There choose.files('*.sens') works the way you're expecting (except it's interactive). Duncan Murdoch
On Thu, 29 Jan 2004, Marcelo Luiz de Laia wrote:> Hi All, > > I have a very simple problem. I have several files in a same directory. > I would like to send for an object only the files that finish in > ".sens.". I execute the command below, > > files <- dir(pattern="*.sens")see help(regexp) - "period '.' matches any single character" - I think you need to escape the '.' possibly as "\\.sens$".> > but it includes all of the files that have "sens", independent of they > be in the end or in the middle of the name of the file. How could I > solve this? I sought in the html_help but I didn't find similar to this. > > My files > > "script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", > "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", > "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", > "Sen_155_03_R3.sens", "tome2sens_time1sens.txt" > > Tahnks very much > >-- Roger Bivand Econonic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway, voice: +47-55959355, fax: +47-55959393; Roger.Bivand at nhh.no
Terminate the sens with a $ as in dir(pattern = "*.sens$") Marcelo Luiz de Laia <mlaia at fcav.unesp.br> Sent by: r-help-bounces at stat.math.ethz.ch 01/29/2004 11:33 AM To: r-help at stat.math.ethz.ch cc: Subject: [R] Doubt about pattern Hi All, I have a very simple problem. I have several files in a same directory. I would like to send for an object only the files that finish in ".sens.". I execute the command below, files <- dir(pattern="*.sens") but it includes all of the files that have "sens", independent of they be in the end or in the middle of the name of the file. How could I solve this? I sought in the html_help but I didn't find similar to this. My files "script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", "Sen_155_03_R3.sens", "tome2sens_time1sens.txt" Tahnks very much -- Marcelo Luiz de Laia, M.Sc. Dep. de Tecnologia, Lab. Bioqu?mica e de Biologia Molecular Universidade Estadual Paulista - UNESP Via de Acesso Prof. Paulo Donato Castelane, Km 05 14.884-900 - Jaboticabal, SP, Brazil PhoneFax: 16 3209-2675/2676/2677 R. 202/208/203 (trab.) HomePhone: 16 3203 2328 - www.lbm.fcav.unesp.br - mlaia at yahoo.com ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Hi. 1. A period in a pattern (as you wrote) means that you want to match *any* character. You need to escape the period, i.e. "\\.", or alternatively use the "[<set>]" indicator where <set> is all the characters you allow at that position, i.e. "[.]". (I prefer the latter because in is more readable and you don't have the \\ or \ problem when cut'n'pasting.) 2. To match the end of string use "$". Thus, you want to do files <- dir(pattern="*[.]sens$") To match filenames that ends with ".sens". This is how regexpr(), gsub() and friends all work. Cheers Henrik Bengtsson> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Marcelo Luiz de Laia > Sent: den 29 januari 2004 15:33 > To: r-help at stat.math.ethz.ch > Subject: [R] Doubt about pattern > > > Hi All, > > I have a very simple problem. I have several files in a same > directory. I would like to send for an object only the files > that finish in ".sens.". I execute the command below, > > files <- dir(pattern="*.sens") > > but it includes all of the files that have "sens", > independent of they be in the end or in the middle of the > name of the file. How could I solve this? I sought in the > html_help but I didn't find similar to this. > > My files > > "script_sens.txt", "Sen_155_01_R1.sens", > "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", > "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", > "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", > "Sen_155_03_R2.sens", "Sen_155_03_R3.sens","tome2sens_time1sens.txt"> > Tahnks very much > > -- > Marcelo Luiz de Laia, M.Sc. > Dep. de Tecnologia, Lab. Bioqu?mica e de Biologia Molecular > Universidade Estadual Paulista - UNESP Via de Acesso Prof. > Paulo Donato Castelane, Km 05 14.884-900 - Jaboticabal, SP, Brazil > PhoneFax: 16 3209-2675/2676/2677 R. 202/208/203 (trab.) > HomePhone: 16 3203 2328 - www.lbm.fcav.unesp.br - mlaia at yahoo.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailma> n/listinfo/r-help > PLEASE > do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Hoi Marcelo, --On donderdag 29 januari 2004 11:33 -0300 Marcelo Luiz de Laia <mlaia at fcav.unesp.br> wrote:> > files <- dir(pattern="*.sens") > > but it includes all of the files that have "sens", independent of they be > in the end or in the middle of the name of the file. >That's because your pattern is a regular expression and not a Windows/DOS wildcard. You'll need something like files <- dir(pattern="\.sens$") \. matches the dot itself (without the slash it's a wildcard for any character) and the dollar sign $ matches the end of the filename. So this way you'll get every file that has 'sens' as its extension regards, Paul -- Paul Lemmens NICI, University of Nijmegen ASCII Ribbon Campaign /"\ Montessorilaan 3 (B.01.03) Against HTML Mail \ / NL-6525 HR Nijmegen X The Netherlands / \ Phonenumber +31-24-3612648 Fax +31-24-3616066
what about dir(pattern="\\.sens+$") HIH Ste On Thu, Jan 29, 2004 at 11:33:25AM -0300, Marcelo Luiz de Laia wrote:> Hi All, > > I have a very simple problem. I have several files in a same directory. I would like to send for an object only the files that finish in ".sens.". I execute the command below, > > files <- dir(pattern="*.sens") > > but it includes all of the files that have "sens", independent of they be in the end or in the middle of the name of the file. How could I solve this? I sought in the html_help but I didn't find similar to this. > > My files > > "script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", "Sen_155_03_R3.sens", "tome2sens_time1sens.txt" > > Tahnks very much > > -- > Marcelo Luiz de Laia, M.Sc. > Dep. de Tecnologia, Lab. Bioqu?mica e de Biologia Molecular > Universidade Estadual Paulista - UNESP > Via de Acesso Prof. Paulo Donato Castelane, Km 05 > 14.884-900 - Jaboticabal, SP, Brazil > PhoneFax: 16 3209-2675/2676/2677 R. 202/208/203 (trab.) > HomePhone: 16 3203 2328 - www.lbm.fcav.unesp.br - mlaia at yahoo.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Marcelo Luiz de Laia wrote:> I have a very simple problem. I have several files in a same > directory. I would like to send for an object only the files > that finish in ".sens.". I execute the command below, > > > files <- dir(pattern="*.sens")That's not even a valid regular expression in most applications, but "dir" does allow it, for some reason. Anyway, I think you are mixing globs and regular expressions. If "*.sens" is a glob, then it will find all files ending with ".sens", which is not the same as ".sens." which you said above. Stefano Calza <stecalza at tiscali.it> wrote:> > dir(pattern="\\.sens+$")That will find files ending with ".sens", ".senss", ".sensss", etc. To find files ending with ".sens.", the regex is dir(pattern = "\\.sens\\.$") If there is a file called just ".sens.", you have to use "all.files = TRUE", since files beginning with a "." are invisible. Peter -- Peter J. Acklam - pjacklam at online.no - http://home.online.no/~pjacklam
Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:>Peter J. Acklam wrote: > > > Marcelo Luiz de Laia wrote: > > > > > > files <- dir(pattern="*.sens") > > > > That's not even a valid regular expression in most applications, > > but "dir" does allow it, for some reason. Anyway, I think you > > It is a valid regex in GNU's regex code as used by R, and all the GNU > and non-GNU applications I tried accepted it. `*' matches itself when > not used as a repetition qualifier. (I tried several greps, including > those claiming strict POSIX compliance.) > > So, can you please list the `most applications' you tried or give a > reference for your assertion?I should have said "many applications", not "most applications." I use Solaris, and I don't know one Solaris application which allows it, including egrep, oawk, and nawk. And no version of Perl allows it. GNU-tools allow it, but in an inconsistent way. GNU Emacs treats `*x' as `\*x' (match a literal star and a literal x), but GNU grep (version 2.5) treats `*x' as `.*x' (match anything up to and including the first x), which is something quite different. It's a mess, it's inconsistent, and it's not portable. I suggest people stop using "*..." and use "\*..." or ".*..." depending on what is wanted. Peter -- Peter J. Acklam - pjacklam at online.no - http://home.online.no/~pjacklam
Your question has by now been answered but I thought I would add that if you want to do it via file globbing on Windows rather than regular expressions then this function would help: list.files.glob <- function( spec ) { # returns list of files or NULL if none: spec uses globbing and can # use either forward or backward slashes. # e.g list.files.glob( "c:/a*.*" ) # e.g. list.files.glob( "c:/myfolder/my*.dat") # only works on Windows if ( substring(spec,2,2) != ":" ) spec <- paste( "C", spec, sep= ":" ) z <- system( paste("cmd /c attrib", spec), intern = T, invisible = T) if ( !pmatch("File not found - ", z[1], nomatch = 0) ) substring(z,12) } For example: list.files.glob( "*.sens" ) On Thu, Jan 29, 2004 at 11:33:25AM -0300, Marcelo Luiz de Laia (mlaia at fcav.unesp.br) wrote:> Hi All, > > I have a very simple problem. I have several files in a same directory. I would like to send for an object only the files that finish in ".sens.". I execute the command below, > > files <- dir(pattern="*.sens") > > but it includes all of the files that have "sens", independent of they be in the end or in the middle of the name of the file. How could I solve this? I sought in the html_help but I didn't find similar to this. > > My files > > "script_sens.txt", "Sen_155_01_R1.sens", "Sen_155_01_R2.sens", "Sen_155_01_R3.sens", "Sen_155_02_R1.sens", "Sen_155_02_R2.sens", "Sen_155_02_R3.sens", "Sen_155_03_R1.sens", "Sen_155_03_R2.sens", "Sen_155_03_R3.sens", "tome2sens_time1sens.txt" > > Tahnks very much > > -- > Marcelo Luiz de Laia, M.Sc. > Dep. de Tecnologia, Lab. BioquĂmica e de Biologia Molecular > Universidade Estadual Paulista - UNESP > Via de Acesso Prof. Paulo Donato Castelane, Km 05 > 14.884-900 - Jaboticabal, SP, Brazil > PhoneFax: 16 3209-2675/2676/2677 R. 202/208/203 (trab.) > HomePhone: 16 3203 2328 - www.lbm.fcav.unesp.br - mlaia at yahoo.com >