简体   繁体   中英

Extracting Elements from character vector

in my set-up I have these variables:

loc <- c('anf', 'iqq', 'stg', 'ccp', 'coy', 'puq')
subDirs <- list.dirs(full.names = FALSE)

In the current directory from where I extract sub-directories names, I have some sub-directories in which I data I am interested in processing. But there are also some other sub Directories that I cannot delete because they contain other important files. The sub-dirs I am interested in have the particularity that each one starts with a "code" which can be either: anf, iqq, stg, ccp, coy, puq. Therefore, I want to extract sub-dirs names that contains those codes at the first 3 characters of subdirs name.

for (k in 1:length(subDirs)){
    if (!substr(subDirs[k], 1, 3) %in% loc & !is.na(subDirs[k])){
        subDirs <- subDirs[subDirs != subDirs[k]]
    }
}

For instance, I Have

[1] ""               "anf_b2_results" "anf_c1_results" "anf_c2_results" "anf_c3_results"
 [6] "anf_e1_results" "anf_e2_results" "iqq_b2_results" "iqq_e1_results" "iqq_e2_results"
[11] "poi_library"    "stg_b2_results" "stg_e1_results" "stg_e2_results"

I want:

[1] "anf_b2_results" "anf_c1_results" "anf_c2_results" "anf_c3_results" "anf_e1_results"
 [6] "anf_e2_results" "iqq_b2_results" "iqq_e1_results" "iqq_e2_results" "stg_b2_results"
[11] "stg_e1_results" "stg_e2_results"

I did that for extracting the names I am interested in, but is there a better (efficient, easier) way? I have the feeling that what I did is too much hardcoding and I want an efficient code, because there will be lots of sub-dirs later.

Best Regards

You don't need any for loops. substr is vectorized. Just do:

subDirs[substr(subDirs, 1, 3) %in% loc]
# [1] "anf_b2_results" "anf_c1_results" "anf_c2_results" "anf_c3_results" "anf_e1_results"
# [6] "anf_e2_results" "iqq_b2_results" "iqq_e1_results" "iqq_e2_results" "stg_b2_results"
# [11] "stg_e1_results" "stg_e2_results"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM