简体   繁体   中英

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.

For future reference, you can use traceback() to identify more closely what line of code or function call threw the error.

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. I suspect you don't want them to be factors.

Perhaps, alternatively, you didn't read the man page ?match . In particular,

match(x, table, nomatch = NA_integer_, incomparables = NULL)

Arguments

nomatch the value to be returned in the case when no match is found. Note that it is coerced to integer.

So you're getting an NA now and then which sapply doesn't want. set nomatch to some other value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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