This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use directories as index of a vector?

I'd like to create a vector in which I should put some directories inside.

For example, when I ran the list.dirs(), I found the following list of directories:

 [1] "/home/platone1/MethylationData/_EPIPOLE_/"
 [2] "/home/platone1/MethylationData/_EPIPOLE_//20210304_QEIC21-03-2_pt1_1-12"
 [3] "/home/platone1/MethylationData/_EPIPOLE_//20210304_QEIC21-03-2_pt1_1-12/20210322_QEIC21-03-2_piastra1"
 [4] "/home/platone1/MethylationData/_EPIPOLE_//20210304_QEIC21-03-2_pt1_1-12/20210322_QEIC21-03-2_piastra1/Data"
 [5] "/home/platone1/MethylationData/_EPIPOLE_//20210304_QEIC21-03-2_pt1_1-12/20210322_QEIC21-03-2_piastra1/Data/Bookmark Analyses"
 [6] "/home/platone1/MethylationData/_EPIPOLE_//20210304_QEIC21-03-2_pt1_1-12/IDAT"
 [7] "/home/platone1/MethylationData/_EPIPOLE_//20210310_QEIC21-03-2_pt2_1-12"
 [8] "/home/platone1/MethylationData/_EPIPOLE_//20210310_QEIC21-03-2_pt2_1-12/20210312_QEIC21-03-2_piastra2"
 [9] "/home/platone1/MethylationData/_EPIPOLE_//20210310_QEIC21-03-2_pt2_1-12/20210312_QEIC21-03-2_piastra2/Data"
[10] "/home/platone1/MethylationData/_EPIPOLE_//20210310_QEIC21-03-2_pt2_1-12/IDAT"
[11] "/home/platone1/MethylationData/_EPIPOLE_//20210326_QEIC21-03-3_pt3_1-12"
[12] "/home/platone1/MethylationData/_EPIPOLE_//20210326_QEIC21-03-3_pt3_1-12/20210326_QEIC21-03-3_piastra3"
[13] "/home/platone1/MethylationData/_EPIPOLE_//20210326_QEIC21-03-3_pt3_1-12/20210326_QEIC21-03-3_piastra3/Data"
[14] "/home/platone1/MethylationData/_EPIPOLE_//20210326_QEIC21-03-3_pt3_1-12/IDAT"
....

If I wanted to extract only directories which finish with "/IDAT" and put them inside a vector like indices of the same vector, how could I do?

r

3 answers

base R

dirs <- list.dirs()
matches <- dirs[grep("IDAT$", dirs)]

stringr

library("stringr")

matches <- str_subset(list.dirs(), "IDAT$")
grep("/IDAT$", list.dirs(), value = TRUE)

This will use grep to check, with the regex meaning that only directories will be returned that end with /IDAT (that is what the $ means). The value=TRUE then returns the value rather than the index of the matches so the actual directory names.

> dir(pattern = "idat$",  include.dirs = T, recursive = T, full.names = T)
[1] "./ab_test/idat" "./d_test/idat" 

> dir(pattern = "idat$",  include.dirs = T, recursive = T, full.names = T, ignore.case = T)
[1] "./ab_test/idat" "./b3_test/IDAT" "./d_test/idat" 

Log in to answer this question.