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