I have a data frame like this
X <- matrix(rexp(30, rate=.1), ncol=5)
Y <- matrix(rexp(6, rate=.1), ncol=1)
mydata <- data.frame(cbind(Y,X))
Now I want to change the third column of X with each of the columns of Xm
Xm <- matrix(rexp(60, rate=.1), ncol=10)
and do a linear regression and save the sd of X3. If I want to do it manually I do
X[ , 3] <- Xm[ , 1]
mydata <- data.frame(cbind(Y,X))
fit1 = lm(Y~.,data=mydata)
then
X[ , 3] <- Xm[ , 2]
mydata <- data.frame(cbind(Y,X))
fit2 = lm(Y~.,data=mydata)
then
X[ , 3] <- Xm[ , 3]
mydata <- data.frame(cbind(Y,X))
fit3 = lm(Y~.,data=mydata)
.
.
.
etc.
however, if I do it manually it takes so long and not efficient. Is there anyone who can help me to make it more automatic?