2014-12-06 87 views
0

我对R相对较新,我正在做一些dna序列分析。如果序列中没有N,我想知道如何让表函数返回零。而不是返回零它返回下标越界。我可以做一个if语句,但我认为可能有一个非常简单的方法来解决这个问题?谢谢您的帮助!如果值不存在,则R中的表()需要返回零

library(seqinr) 
firstSet<-read.fasta("NC_000912.fna") 
seqFirstSet<-firstSet[[1]] 
length(seqFirstSet) 
count(seqFirstSet,1) 
count(seqFirstSet,2) 
seqTable<-table(seqFirstSet) 
seqTable[["g"]] 
seqTable[["n"]] 
+1

什么是class(s​​eqFirstSet)?请包括足够的示例数据,使您的问题[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – MrFlick 2014-12-06 01:15:54

+0

也许如果你从'dput(head(seqTable))发布输出',事情就会被澄清。 – 2014-12-06 01:52:39

回答

2

如果你的数据是适当水平的因素,那么你不会有任何问题:

> x <- factor(letters[1:3]) 
> y <- factor(letters[1:3], levels = letters) 

> table(x) 
x 
a b c 
1 1 1 

> table(y) 
y 
a b c d e f g h i j k l m n o p q r s t u v w x y z 
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

> table(x)[["g"]] 
Error in table(x)[["g"]] : subscript out of bounds 

> table(y)[["g"]] 
[1] 0 

只需设置levels

相关问题