-5

I currently have two data tables one of which contains independent and control variables in columns while the other contains rows of dependent variables.

Can anybody help in creating a method to do linear models from the two tables that repeats for each row in the dependent value table?

Jautis
  • 459
  • 6
  • 13
  • What have you attempted? How about posting some sample data? [Here's](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) a good place to start. – nrussell Sep 01 '14 at 20:50
  • 2
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. Your present description is quite vague. A concrete example would help greatly. – MrFlick Sep 01 '14 at 20:50

1 Answers1

2

You haven't provided nor a reproducible example, nor the desired output, so I'll have to guess

If this is your column names vector

vec <- LETTERS[1:3]

And this is your data set

set.seed(1)
df <- data.frame(A = sample(10, 10), 
                 B = sample(20, 10),
                 C = sample(30, 10))

Then you can try something like

lapply(vec, 
       function(x) lm(as.formula(paste(x, "~", 
                                       paste(setdiff(names(df), x), 
                                                     collapse = "+"))), 
                      data = df))

Which will give

# [[1]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            B            C  
# 4.9687       0.2410      -0.1565  
# 
# 
# [[2]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            A            C  
# 2.7975       0.8182       0.2775  
# 
# 
# [[3]]
# 
# Call:
#   lm(formula = as.formula(paste(x, "~", paste(setdiff(names(df), 
#                                                       x), collapse = "+"))), data = df)
# 
# Coefficients:
#   (Intercept)            A            B  
# 13.200       -1.675        0.875  
David Arenburg
  • 91,361
  • 17
  • 137
  • 196