2

I have a vector of file extensions like (.txt,.TXT,.csv,.xls), i tried "\\.(TXT|txt|csv|xls)$" according to this which return TRUE if any of the extensions are present, however i am interested to return a TRUE if all the extensions are present in the vector otherwise FALSE. Thanks

Community
  • 1
  • 1
Hashim
  • 307
  • 1
  • 5
  • 16

1 Answers1

4

Try

 v1 <- c('a1.txt', 'a2.TXT', 'a3.csv', 'a22.txt', 'a13.TXT', 'a23.txt')
 ext <- c('txt', 'TXT', 'csv', 'xls')
 all(ext %in% sub('.*\\.', '', v1) )
 #[1] FALSE
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Still 4 mins to go :):) – Hashim Jun 12 '15 at 17:08
  • If i need to check if `txt` or`TXT` and `csv` and `xls`, how can i do that – Hashim Jun 22 '15 at 07:02
  • @Hashim Isn't that the condition in the question or do you need `any` in place of `all`? – akrun Jun 22 '15 at 07:04
  • @Hashim Based on the example showed, what do you want to return – akrun Jun 22 '15 at 07:06
  • i mean can i do `ignore.case ` here with `all` – Hashim Jun 22 '15 at 07:06
  • @Hashim If I use `all(grepl('.txt|.csv|.xls', v1,ignore.case=TRUE))`, I get `TRUE` for `v1`. But note that `.xls` is not in `v1`. So, do you want the result to be `FALSE` – akrun Jun 22 '15 at 07:07
  • I think it will give true if any of the three are present , but i want if all the three are present with both the cases – Hashim Jun 22 '15 at 07:12
  • @Hashim Try `all(sapply(c('.txt', '.csv', '.xls'), function(x) any(grepl(x, v1, ignore.case=TRUE))))# [1] FALSE` – akrun Jun 22 '15 at 07:13
  • I have an issue with v1 <- `c("GSE10952_Colon_cancer_cells_cell_line_nonorm_nobkgd_GEOarchive_Matrix.txt","GSE10952_Colon_cancer_cells_cell_line_nonorm_nobkgd_GEOarchive_Matrix1.txt")`, which gives `TRUE`, how its ?? – Hashim Jun 22 '15 at 08:19
  • I get `FALSE` for the example you provided. i.e `all(sapply(c('.txt', '.csv', '.xls'), function(x) any(grepl(x, v1, ignore.case=TRUE)))) #[1] FALSE` – akrun Jun 22 '15 at 08:21
  • I too got `FALSE` , what about the above two lines – Hashim Jun 22 '15 at 08:23
  • @Hashim I am talking about the `v1 <- c("GSE10952_...` – akrun Jun 22 '15 at 08:24
  • can u plz try `all(sapply(c('.CEL', '.txt'), function(x) any(grepl(x, v1, ignore.case=TRUE))))` – Hashim Jun 22 '15 at 08:26
  • @Hashim Now I understand the reason, you have `cell` inside the text which matched with `cel` and `.` is just any character. Try `all(sapply(c('\\.CEL', '\\.txt'), function(x) any(grepl(x, v1, ignore.case=TRUE))))# [1] FALSE` or make it more specific `all(sapply(c('\\.CEL$', '\\.txt$'), function(x) any(grepl(x, v1, ignore.case=TRUE)))) [1] FALSE` – akrun Jun 22 '15 at 08:30
  • Oh my bad !!, yes that is the reason, sorry for asking here. – Hashim Jun 22 '15 at 08:33
  • @Hashim It's okay, that question looked good and would have gained me some reputation points :-) – akrun Jun 22 '15 at 08:35