Search code examples
rmapplycombn

R: how can I reference the corresponding column between a combination of names within a dataframe?


I've managed to make different combinations of 2 from a set of data so that I get the pairs of names within the data set. I would like to have a function when I use mapply so that I can use each name in each pair to reference their corresponding datasets. Right now I have:

myPairs <- combn(names(iris[1:4]), 2)

f <- function(x,y)
{
#Want to make a lm(x ~ y) and other potential calculations 
}

a <- myPairs[1,]
b <- myPairs[2,]

mapply(f, a, b)

In other words, I want to calculate it like so:

data <- iris
attach(data)
lm(Sepal.Length ~ Sepal.Width)
lm(Sepal.Length ~ Petal.Length)
lm(Sepal.Length ~ Petal.Width)
lm(Sepal.Width ~ Petal.Length)
lm(Petal.Length ~ Petal.Width)

but using the names from the combn as reference to the dataset


Solution

  • First off, don't attach your data. Use the data argument for lm instead.

    Let's build the formulas:

    myFormula = apply(myPairs, MARGIN = 2, FUN = paste, collapse = " ~ ")
    

    Then make the models

    myModels = lapply(myFormula, function(x) lm(formula = x, data = iris))
    

    We can then inspect some results:

    myModels[[1]]
    # Call:
    # lm(formula = x, data = iris)
    #
    # Coefficients:
    # (Intercept)  Sepal.Width  
    #      6.5262      -0.2234