1

我有一个文件夹,其中包含一组不同的数据文件。我想计算包含给定术语(如“25”或“颜色编码”)的文件数量,如果可能,列出这些文件的名称。他们有什么方法可以在 R 中做到这一点吗?

4

1 回答 1

1

这能满足你的需要吗

findTermsInFileNames <- function(terms, theFolder="path/to/Folder/", extension="R", ignoreCase=TRUE)  {
  # Iterates through all files of type `extension` in `theFolder` and returns a 
  #  count for each time one of `terms` appears in a file name
  # Note:  extension should NOT include a dot.  good: "*"  bad: ".*"

  # str_detect is from stringr
  require(stringr)

  # Get list of files
  pat <- paste0("*.", extension)
  filesList <- list.files(path.expand(theFolder), pattern=pat, ignore.case=ignoreCase)

  # Add attribute to terms, whether cAseS should be ignored
  attr(terms, "ignore.case") <- ignoreCase

  # Tabulate all occurrences of temrs in the list of file names
  results <- rowSums(sapply(filesList,  str_detect, terms, USE.NAMES=TRUE)) 

  # Clean up the table names
  names(results) <- terms

  return(results)
}

例子:

fold <- "~/git/src"
terms <- c("an", "example", "25")

findTermsInFileNames(terms, fold)
于 2012-11-12T17:47:39.857 回答