0

I am relatively new to R, and I'm doing some dna sequence analysis. I wanted to know how to get the table function to return a zero if there are no N's in the sequence. Instead of returning zero it returns subscript out of bounds. I could do an if statement but I thought there might be a really simple way to fix this? Thanks for the help!

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"]]
smci
  • 32,567
  • 20
  • 113
  • 146
trhood
  • 13
  • 4
  • 1
    What is `class(seqFirstSet)`? Please include enough sample data to make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Dec 06 '14 at 01:15
  • Perhaps if you posted output from `dput(head(seqTable))` things would be clarified. – IRTFM Dec 06 '14 at 01:52

1 Answers1

3

If your data is a factor with appropriate levels, then you'll have no problem:

> 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

Just set the levels!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294