简体   繁体   English

如何修复错误 dim(X) 必须具有正长度 R

[英]How ti fix error dim(X) must have a positive length R

 > tmpselected = input$sel_gene_promoter
         
       
 > tmpmydata_genes = mydata_genes[,match(colnames(tmpgeneids),colnames(mydata_genes),nomatch=0)]
 
 > tmpind = unique(na.omit(c(apply(tmpmydata_genes,2,function(k) match(tmpselected,k)))))
 
Error in apply(tmpmydata_genes, 2, function(k) match(tmpselected, k)) : 
  dim(X) must have a positive length

> tmpmydata_genes 
 [1] gene_id chr1.1083  gene_id g292.t1    gene_id g467.t1    gene_id g624.t1    gene_id chr1.2796 
 [6] gene_id chr1.2959  gene_id chr10.1395 gene_id g1586.t1   gene_id chr11.59   gene_id g1922.t1  
10 Levels: gene_id chr1.1083 gene_id chr1.2796 gene_id chr1.2959 gene_id chr10.1395 ... gene_id g624.t1

> tmpselected
[1] gene_id chr1.1083
10 Levels: gene_id chr1.1083 gene_id chr1.2796 gene_id chr1.2959 gene_id chr10.1395 ... gene_id g624.t1



> class(tmpmydata_genes)
    [1] "factor"
> class(tmpselected)
    [1] "factor"
> is.vector(tmpmydata_genes)
[1] FALSE
> 
> is.vector(tmpselected)
[1] FALSE
> dim(tmpmydata_genes)
NULL
> dim(tmpselected)
NULL

I am missing out the simple fix that is causing the error.我错过了导致错误的简单修复。 could anyone hint to solve.任何人都可以暗示解决。

I'm going to guess you just want match(tmpselected, tmpmydata_genes) and don't need to use apply as match is already vectorized.我猜你只想要match(tmpselected, tmpmydata_genes)并且不需要使用 apply 因为 match 已经矢量化了。

For future reference, you can use traceback() to identify more closely what line of code or function call threw the error.为了将来参考,您可以使用traceback()更仔细地确定是哪一行代码或 function 调用引发了错误。

Two guesses based on what you posted.根据您发布的内容进行了两次猜测。 First, try checking dim(tmpmydata_genes) and dim(tmpselected) to see what is actually in your source objects.首先,尝试检查dim(tmpmydata_genes)dim(tmpselected)以查看源对象中的实际内容。 I suspect you don't want them to be factors.我怀疑你不希望它们成为因素。

Perhaps, alternatively, you didn't read the man page ?match .或者,您可能没有阅读手册页?match In particular,尤其是,

match(x, table, nomatch = NA_integer_, incomparables = NULL)匹配(x,表,不匹配 = NA_integer_,无与伦比 = NULL)

Arguments Arguments

nomatch the value to be returned in the case when no match is found. nomatch未找到匹配项时返回的值。 Note that it is coerced to integer.请注意,它被强制转换为 integer。

So you're getting an NA now and then which sapply doesn't want.所以你时不时地得到一个NA ,而sapply不想要。 set nomatch to some other value.nomatch设置为其他值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM