How to use directories as index of a vector?
3
0
Entering edit mode
2.9 years ago

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 • 681 views
ADD COMMENT
3
Entering edit mode
2.9 years ago

base R

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

stringr

library("stringr")

matches <- str_subset(list.dirs(), "IDAT$")
ADD COMMENT
3
Entering edit mode
2.9 years ago
ATpoint 81k
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.

ADD COMMENT
3
Entering edit mode
2.9 years ago
> 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" 
ADD COMMENT

Login before adding your answer.

Traffic: 2862 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6