Reputation: 833
I am using the following code for listing files from an specific directory:
p<-"my_path"
dir(p)
[1] "00_Iniciar.r" "01_01_Carga_diario.r" "01_Carga_continuos.r"
[4] "02_01_Carga_intervenciones.r" "02_02_Carga_young.r" "02_03_Carga_hrsd.r"
[7] "02_Carga_discretos.r" "03_Carga_eventos.r" "04_graficos.r"
[10] "0x_bin.r" "desktop.ini" "graficos"
How can I list all of them except "desktop.ini" using "pattern" parameter?.
Thanks
Upvotes: 0
Views: 1219
Reputation: 10253
Using grep
like this can also do it:
grep(dir(p), pattern = "^desktop\\.ini$", value = TRUE, invert = TRUE)
Setting value = TRUE
makes grep
return the values corresponding to pattern
matches. Furthermore, invert = TRUE
makes grep
return all values for non-hits.
Alternatively
grep(dir(p), pattern = "desktop.ini", value = TRUE, invert = TRUE, fixed = TRUE)
is equivalent as pointed out by @dww in the comments.
A demonstration that works follows. Exclude all .html
files found in the base package library;
grep(dir(system.file(), recursive = TRUE),
pattern = "\\.html$", value = TRUE, invert = TRUE)
# [1] "CITATION" "demo/error.catching.R" "demo/is.things.R" "demo/recursion.R"
# [5] "demo/scoping.R" "DESCRIPTION" "help/aliases.rds" "help/AnIndex"
# [9] "help/base.rdb" "help/base.rdx" "help/paths.rds" "html/R.css"
#[13] "INDEX" "Meta/demo.rds" "Meta/features.rds" "Meta/hsearch.rds"
#[17] "Meta/links.rds" "Meta/package.rds" "Meta/Rd.rds" "R/base"
#[21] "R/base.rdb" "R/base.rdx" "R/Rprofile"
Upvotes: 4
Reputation: 833
I find the solution.. but I think it can be improved..
dir(p,pattern="^([^d]|d[^e]|de[^s]|des[^k]|desk[^t]|deskt[^o]|deskto[^p])")
Upvotes: 0