0

I have started to use R programming language and I have couple of questions bothering me. I have some background in the shell programming which is pretty easy to learn and use in my opinion. However, I have observed that R language is not so straighforward as it could be.

For example, I have a file called tumor.bam in my working directory. In shell programming I can save it and other .bam files into variable FILE and use it simply by typing;

FILE=./tumor.bam
$FILE

If I want to extract the body of filename and use it somewhere else, I can type;

${FILE%.bam}.bai

My question is: is there same kind of shortcut to handle filenames in the R language? Is there any simple way to perform similar actions in R? I must deal with hundreds of different files and this kind of shortcut would be more than favourable.

Thanks for your help in advance!

Jokhe
  • 1
  • 1
  • 2
    This question is basically this same as this question: http://stackoverflow.com/questions/15073753/regex-return-file-name-remove-path-and-file-extension. See the `?tools::file_path_sams_ext` help page to remove the extension, use `paste0()` to add an extension. Write your own helper function to do a more advanced replace. – MrFlick Aug 03 '16 at 18:26
  • Thanks, I didn't find that question with my search! – Jokhe Aug 03 '16 at 18:27
  • `?file` has some useful functions, surprisingly it doesn't have a link to `list.files()` which is also useful. – Gregor Thomas Aug 03 '16 at 18:28
  • Also see `?dir` for help with your issues. In particular, note that `myFiles <- dir(pattern="\\.bam$")` should work for your first problem. (Note that `list.files` and `dir` are basically synonyms). – lmo Aug 03 '16 at 18:28
  • `File <- "tumor.bam"; sub("\\.bam$", ".bai", File)` or if you kow that .bam only appears at the end then this may be good enough: `sub(".bam", ".bai", File, fixed = TRUE)`. `File` can be a vector of file names and it will still work. – G. Grothendieck Aug 03 '16 at 18:46

0 Answers0