我需要從下面的文件夾的內容中列出唯一的主題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點的乾淨方式是什麼?
@Krysta:不過,請注意,如果找不到特定元素的模式,那麼該元素的原始字符串將不會被修改(如'.')。 – jbaums 2014-09-10 14:03:32