【问题标题】:Error in Function: Must subset rows with a valid subscript vector. Logical subscripts must match the size of the indexed input函数错误:必须使用有效的下标向量对行进行子集化。逻辑下标必须与索引输入的大小匹配
【发布时间】:2020-11-25 21:06:34
【问题描述】:

下面,我正在运行这个函数来将大量文件合并到一个大文件中,同时创建一个成功运行的报告表。这是函数(注意上面定义了各种赋值错误):

mergeFiles <- function(pathRoot, pathToSaveMergedFile, finalFileName){
    folders <- list.dirs(path=pathRoot, recursive=F)
    mergedFiles <- data.frame()
    initialMsg()
    report <- data.frame()
    for (folder in folders){
        ## folder=folders[1]
        files <- list.files(path=folder, pattern='xls.$')
        for (file in files){
            msgFileBeingMerged(file.path(folder,file))
            tmp <- tryCatch(read_excel(path=file.path(folder,file)),
                            error=function(e) {msgError(file.path(folder,file));stop()}, 
                            warning=function(w) msgWarning(file.path(folder,file)) )
            ## tmp <- tryCatch(read.xlsx(file=file.path(folder,file), sheetIndex=1),
            ##                 error=function(e) {msgError(file.path(folder,file));stop()},
            ##                 warning=function(w) msgWarning(file.path(folder,file)) )
            
            ## controling for variable type
            idx=!is.na(tmp$rg)
            tmp=tmp[idx,]
            tmp<-formatVarTypes(tmp) 
           
            tmp.report <- data.frame(file=file.path(folder,file), Rows=nrow(tmp))
            report <- rbind(report, tmp.report)
            
            mergedFiles <- tryCatch(rbind(mergedFiles, tmp),
                                    error=function(e) {msgErrorMergingData(file.path(folder,file));stop()},
                                    warning=function(w) msgWarning(file.path(folder,file)) )                                
            msgFileMerged(file.path(folder,file))
        }
    }

    ## tryCatch(WriteXLS(mergedFiles, ExcelFileName=file.path(path.expand(pathToSaveMergedFile),finalFileName), row.names=F),
    ##          error=function(e) {msgErrorWhenSaving();stop()},
    ##          warning=function(w) "Warning" )
    finalFileName <- file.path(path.expand(pathToSaveMergedFile),finalFileName)

    #finalFileNameCSV = gsub(x=finalFileName, pattern='.xlsx$', replacement='.csv')
    #write.csv(mergedFiles, file=finalFileNameCSV, row.names=F)
    #print(dim(mergedFiles))
    write.xlsx(mergedFiles, file=finalFileName)
    #tryCatch(write.xlsx(mergedFiles, file=finalFileName),
    #         error=function(e) {msgErrorWhenSaving();stop()},
    #         warning=function(w) "Warning" )
    reportFileName <- 'report.xlsx'
    write.xlsx(x=report, file=file.path(path.expand(pathToSaveMergedFile), reportFileName))
    ## WriteXLS(x=report, ExcelFileName=file.path(path.expand(pathToSaveMergedFile), reportFileName))
    reportMsg(mergedFiles, report, pathToSaveMergedFile, reportFileName)
    finalMsg()
}

我不断收到以下错误/回溯:

Error: Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 42 but subscript `c(idx)` has size 0.
 Error: Must subset rows with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 42 but subscript `c(idx)` has size 0.
Run `rlang::last_error()` to see where the error occurred. 
17.
stop(fallback) 
16.
rlang:::signal_abort(x) 
15.
cnd_signal(cnd) 
14.
(function (cnd) 
{
    cnd$subscript_arg <- i_arg
    cnd$subscript_elt <- "row" ... 
13.
signalCondition(cnd) 
12.
signal_abort(cnd) 
11.
cnd_signal(new_error_subscript_size(i, n = n, ..., body = cnd_body_vctrs_error_indicator_size)) 
10.
stop_indicator_size(i = i, n = n, subscript_action = subscript_action, 
    subscript_arg = subscript_arg) 
9.
vec_as_location(i, n) 
8.
withCallingHandlers(expr, vctrs_error_subscript = function(cnd) {
    cnd$subscript_arg <- i_arg
    cnd$subscript_elt <- "row"
    if (isTRUE(assign) && !isTRUE(cnd$subscript_action %in% c("negate"))) { ... 
7.
subclass_row_index_errors(vec_as_location(i, n), i_arg = i_arg, 
    assign = assign) 
6.
vectbl_as_row_location(i, nr, i_arg, assign) 
5.
vectbl_as_row_index(i, x, i_arg) 
4.
tbl_subset_row(xo, i = i, i_arg) 
3.
`[.tbl_df`(tmp, c(idx), ) at CLEA-merging-source.R#84
2.
tmp[c(idx), ] at CLEA-merging-source.R#84
1.
mergeFiles(pathRoot, pathToSaveMergedFile, finalFileName) 

我尝试将我的 tmp[c(idx), ] 更改为 tmp[idx, ] 并且(未成功)尝试将其更改为 data.frame 以防这是 ​​tbl 问题(根据 Stack Overflow 的类似帖子。任何关于为什么我会看到这个错误?或者是否需要更多信息来回答这个问题?感谢任何信息。

编辑:回溯上下文,如果有帮助

  1. +-global::mergeFiles(pathRoot, pathToSaveMergedFile, finalFileName)
  2. | +-tmp[idx, ] clea/ReleaseScript/CLEA-merging//CLEA-merging-source.R:85:12
  3. | \-tibble:::`[.tbl_df`(tmp, idx, ) clea/ReleaseScript/CLEA-merging//CLEA-merging-source.R:85:12
  4. |   \-tibble:::tbl_subset_row(xo, i = i, i_arg)
  5. |     \-tibble:::vectbl_as_row_index(i, x, i_arg)
  6. |       \-tibble:::vectbl_as_row_location(i, nr, i_arg, assign)
  7. |         +-tibble:::subclass_row_index_errors(...)
  8. |         | \-base::withCallingHandlers(...)
  9. |         \-vctrs::vec_as_location(i, n)
 10. \-vctrs:::stop_indicator_size(...)

【问题讨论】:

  • 似乎没有为文件定义idx,我怀疑在这种情况下tmp$rg 不存在。尝试在tmp=tmp[idx,] 上方的行中使用browser() 来检查idx 是否存在。
  • 好问题。但是,idx 在函数中定义为我的rg 或区域列的NA 值以外的任何值。此外,我仔细检查了所有文件,不幸的是,每个文件都有一个rg 列。
  • 是的,但以防万一我会检查它。或者您可以在tmp=tmp[idx,] 之前添加print(str(idx)) 以检查发生错误时idx 是什么

标签: r function dataframe vector subscript


【解决方案1】:

你写了一个很长的函数,也许你可以尝试分解成更小的部分并检查每个部分的输出。 str() 或 summary() 之类的函数可能会有所帮助。

我在我的工作流程中遇到了同样的错误,可以使用 as_tibleas.data.frame 之类的函数来规避。喜欢:

df0 &lt;- rbind(df1, df2, df3)

df0 %&gt;% filter(my_var == 2)

我得到了错误。但我可以用as.data.frame 解决它:

df0 %&gt;% as.data.frame() %&gt;% filter(my_var == 2)

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2021-07-02
    • 2016-06-27
    • 2012-12-29
    • 2014-09-25
    相关资源
    最近更新 更多