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
Asked
Active
Viewed 40 times
2
-
i tried `all` but it returns `TRUE` always if any of the extension is present. – Hashim Jun 12 '15 at 17:02
1 Answers
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
-
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 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
-
-
-
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
-
-
@Hashim It's okay, that question looked good and would have gained me some reputation points :-) – akrun Jun 22 '15 at 08:35