Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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 编写一个函数来过滤目录中的多个excel文件,并返回结果_R_Excel_Function_Dataframe - Fatal编程技术网

R 编写一个函数来过滤目录中的多个excel文件,并返回结果

R 编写一个函数来过滤目录中的多个excel文件,并返回结果,r,excel,function,dataframe,R,Excel,Function,Dataframe,我的目录中有多个格式相同的excel文件。我想编写一个函数,读取这些文件并按图所示过滤它们,然后将所有行rbinds到最后一个数据帧。 最好有一个id列来标识这些行来自哪个文件 library(readxl) apply_fun <- function(filename) { data <- readxl::read_excel(filename, sheet = "Section Cut Forces - Design",

我的目录中有多个格式相同的excel文件。我想编写一个函数,读取这些文件并按图所示过滤它们,然后将所有行rbinds到最后一个数据帧。 最好有一个id列来标识这些行来自哪个文件

library(readxl)

apply_fun <- function(filename) {
data <- readxl::read_excel(filename, sheet = "Section Cut Forces - Design", 
                           skip = 1, col_names = TRUE) ; data <- data[-1,] ;

data <- subset(data, "StepTye" == "Max")                                  
Moment <- data[,c(1,2,6,10)]

Moment
}

filenames <- list.files(pattern = "\\.xlsx", full.names = TRUE)
out <- data.frame(t(sapply(filenames, apply_fun)))
库(readxl)

试试这个。使用
do.call()。代码如下:

library(readxl)
#Function
apply_fun <- function(filename) {
  data <- readxl::read_excel(filename, sheet = "Section Cut Forces - Design", 
                             skip = 1, col_names = TRUE)
  data <- data[-1,]
  data <- subset(data, StepType == "Max")
  Moment <- data[,c(1,2,6,10)]
  Moment$id <- filename
  return(Moment)
}
#Code
filenames <- list.files(pattern = "\\.xlsx", full.names = TRUE)
L <- lapply(filenames,apply_fun)
#Bind
Out <- do.call(rbind,L)
库(readxl)
#作用

申请乐趣谢谢你的回复,我可以看到你在做什么,但我尝试申请时遇到了一个错误。错误是“
$@MaralDorri中出现错误您能否在可下载链接中共享excel文件以再次测试该函数?”?dput无法工作,因为excel文件需要检查。我刚刚用谷歌驱动器更新了原始帖子link@MaralDorri我添加了一个更新,并使用提供的文件进行了测试。让我知道这是否对你有效!我刚刚检查了更新,当我删除“from
StepType
时,我在eval(e,x,parent.frame())中得到了错误”:未找到对象“StepTye”我不确定它是如何为您运行的。。。
head(Out)
# A tibble: 6 x 5
  SectionCut OutputCase V2                 M3                 id             
  <chr>      <chr>      <chr>              <chr>              <chr>          
1 1          LL-3       35.317999999999998 51208.425000000003 ./84-9-S00.xlsx
2 1          LL-2       35.917999999999999 48494.190999999999 ./84-9-S00.xlsx
3 1          LL-1       26.937999999999999 33676.78           ./84-9-S00.xlsx
4 2          LL-3       37.884999999999998 14.86              ./84-9-S00.xlsx
5 2          LL-2       32.731999999999999 14.372999999999999 ./84-9-S00.xlsx
6 2          LL-1       23.114999999999998 10.484             ./84-9-S00.xlsx
# A tibble: 35 x 5
   SectionCut OutputCase V2                 M3                 id             
   <chr>      <chr>      <chr>              <chr>              <chr>          
 1 1          LL-3       35.317999999999998 51208.425000000003 ./84-9-S00.xlsx
 2 1          LL-2       35.917999999999999 48494.190999999999 ./84-9-S00.xlsx
 3 1          LL-1       26.937999999999999 33676.78           ./84-9-S00.xlsx
 4 2          LL-3       37.884999999999998 14.86              ./84-9-S00.xlsx
 5 2          LL-2       32.731999999999999 14.372999999999999 ./84-9-S00.xlsx
 6 2          LL-1       23.114999999999998 10.484             ./84-9-S00.xlsx
 7 3          LL-3       38.220999999999997 57100.021000000001 ./84-9-S00.xlsx
 8 3          LL-2       37.587000000000003 53050.275999999998 ./84-9-S00.xlsx
 9 3          LL-1       24.864999999999998 36847.82           ./84-9-S00.xlsx
10 Left       LL-3       31.035             49882.42           ./84-9-S00.xlsx
# ... with 25 more rows