【发布时间】:2014-09-10 21:58:59
【问题描述】:
我需要从以下文件夹的内容中获取唯一主题 ID 的列表(_ 之前和 / 之后的部分)。
[1] "." "./4101_0" "./4101_0/4101 Baseline"
[4] "./4101_1" "./4101_2" "./4101_2_2"
[7] "./4101_3" "./4101_4" "./4101_5"
[10] "./4101_6"
现在我正在这样做(使用包 stringr 和 foreach)。
# Create list of contents
Folder.list <- list.dirs()
# Split entries by the "/"
SubIDs <- str_split(Folder.list, "/")
# For each entry in the list, retrieve the second element
SubIDs <- unlist(foreach(i=1:length(SubIDs)) %do% SubIDs[[i]][2])
# Split entries by the "_"
SubIDs <- str_split(SubIDs, "_")
# Take the second element after splitting, unlist it, find the unique entries, remove the NA and coerce to numeric
SubIDs <- as.numeric(na.omit(unique(unlist(foreach(i=1:length(SubIDs)) %do% SubIDs[[i]][1]))))
这可以完成工作,但似乎不必要地可怕。从 A 点到 B 点有什么更清洁的方式?
【问题讨论】:
标签: r