Question

Im trying to apply an simple formula y~x to a function, in this case adonis {vegan} when inside mapply but I get Error: object of type 'symbol' is not subsettable.

Im running: mapply(adonis, formula=dist_list~group_list, data=group_list, SIMPLIFY=F), where dist_list is a list of distance matrices in dist class and group_list a list of factors. The same works with single dist and factor objects. This also works: mapply(betadisper, d = dist_list, group = group_list, SIMPLIFY=FALSE).

So the problem seem to be with the use of a formula inside mapply. I've been trying the use of substitute but ending up with the same error.

Here's an attempt of providing a reproducible example:

data(varespec)

dis <- vegdist(varespec, method="bray")
dis1 <- vegdist(varespec,method="jaccard")
dis2 <- vegdist(varespec,method="mountford")

dist_list <- list(dis,dis1,dis2)

groups <- factor(c(rep(1,16), rep(2,8)), labels = c("grazed","ungrazed"))
groups1 <- factor(c(rep(1,10), rep(2,14)), labels = c("grazed","ungrazed"))
groups2 <- factor(c(rep(1,20), rep(2,4)), labels = c("grazed","ungrazed"))

group_list <- list(groups,groups1,groups2)

mapply(adonis, formula=dist_list~group_list, data=group_list, SIMPLIFY=F)

#Error: object of type 'symbol' is not subsettable

Any pointer to sort this out would be very much appreciated, thanks!

Was it helpful?

Solution 2

This seems to work though

mapply(function(x,y) adonis(x~y), dist_list, group_list, SIMPLIFY=FALSE)

OTHER TIPS

You need to use the MoreArgs argument to pass additional arguments to adonis when using mapply. see the mapply documentation. Something like (untested):

mapply(
  adonis, group_list=group_list, dist_list=dist_list, 
  MoreArgs=list(formula=dist_list~group_list), 
  SIMPLIFY=F
)

Otherwise mapply is going to attempt to cycle through your formula as it were a list/vector, which is what is causing the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top