Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算R中包含给定值或术语的文件数_R - Fatal编程技术网

计算R中包含给定值或术语的文件数

计算R中包含给定值或术语的文件数,r,R,我有一个文件夹,里面有一组不同的数据文件。我想计算包含给定术语的文件的数量,如“25”或“颜色编码”,如果可能,列出这些文件的名称。他们有没有办法在R中做到这一点?这能满足您的需要吗 findTermsInFileNames <- function(terms, theFolder="path/to/Folder/", extension="R", ignoreCase=TRUE) { # Iterates through all files of type `extension`

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

这能满足您的需要吗

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)
}

findTermsInFileNames这能满足您的需要吗

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)
}

findtermsinfilename是的,我肯定有。(虽然,作为一个提示,如果您试图在文件中搜索给定的字符串,这种任务不太适合R。)最好的方法在很大程度上取决于文件的格式。有帮助吗?是的,我肯定有。(虽然,作为提示,如果您试图在文件中搜索给定的字符串,这种任务并不适合R。)最好的方法很大程度上取决于文件的格式。有帮助吗?