0

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.

LEE
  • 316
  • 2
  • 8

1 Answers1

3

Try this tidyverse approach. You want to transform your data to wide format. So you can create an unique id and then use pivot_wider(). Also, as rownames must be considered, you can use rownames_to_column to create a new variable named Coef:

library(tidyverse)
#Code
df %>% rownames_to_column('Coef') %>%
  mutate(id=1) %>%
  pivot_wider(names_from = Coef,values_from=starts_with('SWC')) %>%
  select(-id)

Output:

# A tibble: 1 x 12
  `SWC_11_(Interc~ SWC_11_day `SWC_12_(Interc~ SWC_12_day `SWC_13_(Interc~ SWC_13_day
             <dbl>      <dbl>            <dbl>      <dbl>            <dbl>      <dbl>
1           10472.   -6.56e-6            4063.   -2.53e-6           75041. -0.0000473
# ... with 6 more variables: `SWC_14_(Intercept)` <dbl>, SWC_14_day <dbl>,
#   `SWC_21_(Intercept)` <dbl>, SWC_21_day <dbl>, `SWC_22_(Intercept)` <dbl>, SWC_22_day <dbl>

Some data used:

#Data
df <- structure(list(SWC_11 = c(10471.95, -6.563423e-06), SWC_12 = c(4063.32, 
-2.525118e-06), SWC_13 = c(75040.76, -4.726106e-05), SWC_14 = c(7742.763, 
-4.842427e-06), SWC_21 = c(22965.85, -1.443707e-05), SWC_22 = c(21856.88, 
-1.374111e-05)), class = "data.frame", row.names = c("(Intercept)", 
"day"))
Duck
  • 39,058
  • 13
  • 42
  • 84
  • But I have to convert the coefficients from vector to dataframe, fit<-as.data.frame(coef(fit)) – LEE Sep 17 '20 at 09:09