rdataframecbindmanova

How to call columns for cbind using column names stored in a vector (attempting to perform pairwise comparison of columns with manova in R)


I am trying to perform a pairwise manova analysis where I loop through all the possible pairs of my columns. I think this is best communicated with an example:

varList <- colnames(iris)

m1 <- manova(cbind(varList[1], varList[2]) ~ Species, data = iris)
# Error in model.frame.default(formula = cbind(varList[1], varList[2]) ~  :
# variable lengths differ (found for 'Species')

m2 <- manova(cbind(noquote(varList[1]), noquote(varList[2])) ~ Species,
             data = iris)

# Error in model.frame.default(formula = cbind(noquote(varList[1]), noquote(varList[2])) ~  : 
# variable lengths differ (found for 'Species')

m3 <- manova(cbind(Sepal.Length, Petal.Length) ~ Species, data = iris)
m4 <- manova(cbind(iris[ ,1], iris[ ,3]) ~ Species, data = iris)

summary(m3)

#            Df Pillai approx F num Df den Df    Pr(>F)    
# Species     2 0.9885   71.829      4    294 < 2.2e-16 ***
# Residuals 147                                            
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

R.version.string
# [1] "R version 3.4.2 (2017-09-28)"

RStudio.Version()$version
# [1] ‘1.1.383’

I think this is more related to referring to colnames from a vector in my cbind() function. I saw something about the using parenthesis from this question here, but can't get that to work for my case. I can call the columns by their number (see m4), but I'd prefer to use column names if possible.


Solution

  • You need to wrap each of the entries from the vector that you are calling with eval(as.symbol()).

    So: m1 <- manova(cbind(eval(as.symbol(varList[1])), eval(as.symbol(varList[2]))) ~ Species, data = iris) should work.