I got gathered coefficient outputs of several models like below, (note: now the point is the outputs is a series of vectors, not a data frame)
Call:
lm(formula = fct, data = sub)
Coefficients:
SWC_11 SWC_12 SWC_13 SWC_14 SWC_21 SWC_22
(Intercept) 1.047195e+04 4.063320e+03 7.504076e+04 7.742763e+03 2.296585e+04 2.185688e+04
day -6.563423e-06 -2.525118e-06 -4.726106e-05 -4.842427e-06 -1.443707e-05 -1.374111e-05
Now I want to put all the parameters inside one dataframe and named the columns with the same prefix as the above. My expected outputs should be like,
SWC_11_Intercept SWC_11_slope SWC_12_Intercept SWC_12_slope SWC_13_Intercept SWC_13_slope SWC_14_Intercept SWC_14_slope SWC_21_Intercept SWC_21_slope SWC_22_Intercept SWC_22_slope
1.047195e+04 -6.563423e-06 4.063320e+03 -2.525118e-06 7.504076e+04 -4.726106e-05 7.742763e+03 -4.842427e-06 2.296585e+04 -1.443707e-05 2.185688e+04 -1.374111e-05
The stupid way to achieve this is, (ps: the model named 'fit')
SWC_11_intercept<- coef(fit)[1,1]
SWC_11_slope<-coef(fit)[2,1]
SWC_12_intercept<- coef(fit)[1,2]
SWC_12_slope<-coef(fit)[2,2]
SWC_13_intercept<- coef(fit)[1,3]
SWC_13_slope<-coef(fit)[2,3]
SWC_14_intercept<- coef(fit)[1,4]
SWC_14_slope<-coef(fit)[2,4]
But I want to use apply
function to do this, the point is I don't know to write the function part. I also want to create or paste the column names in an easier way, but apparently my current code seems not work. (ps: the data frame 'swc' has the same columns as above except the first column)
parameters <- apply(coef(fit), c(1,2), function(row))
parameter_colnames <- paste(swc[,-1],"_intercept|slope",sep="")
Hope someone could help.