Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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
根据data.frame中的正则表达式条件对列表的多个元素运行单独的函数_R_Regex - Fatal编程技术网

根据data.frame中的正则表达式条件对列表的多个元素运行单独的函数

根据data.frame中的正则表达式条件对列表的多个元素运行单独的函数,r,regex,R,Regex,下面的方法很有效,但我缺少一种函数式编程技术、索引或更好的数据结构化方法。一个月后,需要一点时间来准确地记住它是如何工作的,而不是易于维护。这似乎是一个解决办法,但它不应该。我想使用正则表达式来决定对预期的文件组使用哪个函数。当出现新的文件格式时,我可以编写read函数,然后将该函数和regex一起添加到data.frame中,以便与其他所有文件一起运行 我有不同格式的Excel和csv文件需要读取和标准化。我想维护文件名regex的列表或data.frame以及要使用的适当函数。有时会出现不匹

下面的方法很有效,但我缺少一种函数式编程技术、索引或更好的数据结构化方法。一个月后,需要一点时间来准确地记住它是如何工作的,而不是易于维护。这似乎是一个解决办法,但它不应该。我想使用正则表达式来决定对预期的文件组使用哪个函数。当出现新的文件格式时,我可以编写read函数,然后将该函数和regex一起添加到data.frame中,以便与其他所有文件一起运行

我有不同格式的Excel和csv文件需要读取和标准化。我想维护文件名regex的列表或data.frame以及要使用的适当函数。有时会出现不匹配的新文件格式,以及没有新文件的旧格式。但事情变得复杂了,这是我更愿意避免的

# files to read in based on filename
fileexamples <- data.frame(
  filename = c('notanyregex.xlsx','regex1today.xlsx','regex2today.xlsx','nomatch.xlsx','regex1yesterday.xlsx','regex2yesterday.xlsx','regex3yesterday.xlsx'),
  readfunctionname = NA
)

# regex and corresponding read function
filesourcelist <- read.table(header = T,stringsAsFactors = F,text = "
  greptext readfunction
 '.*regex1.*' 'readsheettype1'
 '.*nonematchthis.*' 'readsheetwrench'
 '.*regex2.*' 'readsheettype2'
 '.*regex3.*' 'readsheettype3'
 ")

# list of grepped files
fileindex <- lapply(filesourcelist$greptext,function(greptext,files){
  grepmatches <- grep(pattern = greptext,x = data.frame(files)[,1],ignore.case = T)
},files = fileexamples$filename)

# run function on files based on fileindex from grep
for(i in 1:length(fileindex)){
  fileexamples[fileindex[[i]],'readfunctionname'] <- filesourcelist$readfunction[i]
}
#基于文件名读入的文件
文件示例