1

I am trying to plot some rarefaction curves, but I am getting this error:

Error in as(x, "matrix")[i, j, drop = FALSE] :  (subscript) logical subscript too long

I am giving a reproducible example on how I use the function:

 data("GlobalPatterns")
rare<-rarecurve(otu_table(GlobalPatterns), step=100, lwd=2, ylab="OTU", label=F)

I am using R.4.2.2 and version 2.6-4 of vegan. I already used this function with the phyloseq object like this and it worked properly, Do you know what is the problem and how I could fix it?

i.b
  • 167
  • 11

1 Answers1

4

It seems to be that phyloseq package does not support this analysis. A way to circumvent these problems is to take care that phyloseq::otu_table returns a normal R matrix. Further, the data you used is transposed to the vegan standard (your data have OTU as rows, whereas they should be columns in vegan standards: observations are rows).

library(phyloseq)
data("GlobalPatterns")
tab <- otu_table(GlobalPatterns)
class(tab) <- "matrix" # as.matrix() will do nothing
## you get a warning here, but this is what we need to have
tab <- t(tab) # transpose observations to rows
library(vegan)
rare <- rarecurve(tab, step=10000, lwd=2, ylab="OTU",  label=F)

Technically the error is that otu_table does not allow long logical index. This is an internal issue of the phyloseq implementation, and I won't delve into innards of phyloseq to find the reason. However, standard R matrix works, but R does not know that otu_table only fakes being a matrix. Therefore the violent way of forcing otu_table to a matrix.

Jari Oksanen
  • 3,287
  • 1
  • 11
  • 15
  • 1
    Thanks a lot for the explanation, I recently updated phyloseq and since then I am getting this error...I will use the solution you gave me! – i.b Nov 22 '22 at 14:54