title: R Storing regression coefficients in data frame column by group
body: <p>I have a data frame with results from a survey. The results are stored in a verticalized format. The data frame looks like this -</p>
  
  <pre><code>set.seed(1000)
  
  df = data.frame(RESP_ID=c(rep(1,6),rep(2,8),rep(3,9),rep(4,5),rep(5,4),rep(6,10),rep(7,4),rep(8,8),rep(9,8),rep(10,10)),
                  CLIENT=c(rep("A",6),rep("A",8),rep("A",9),rep("A",5),rep("A",4),rep("B",10),rep("B",4),rep("B",8),rep("B",8),rep("B",10)),
                  QST=c(paste0("Q",c(1:6)),paste0("Q",c(1:8)),paste0("Q",c(1:9)),paste0("Q",c(1:5)),paste0("Q",c(1:4)),paste0("Q",c(1:10)),paste0("Q",c(1:4)),paste0("Q",c(1:8)),paste0("Q",c(1:8)),paste0("Q",c(1:10))),
                  VALUE=round(runif(72,1,4),0))
  </code></pre>
  
  <p><strong>Description of dataframe</strong></p>
  
  <p>RESP_ID = Respondent ID. Each ID correspondents to a single respondent. In this sample data frame, we have 10 respondents.</p>
  
  <p>CLIENT = Correspondents to the name of the client whose respondents were surveyed. In this sample data frame, we have two clients (A &amp; B).</p>
  
  <p>QST = Corresponds to the question number in the survey.</p>
  
  <p>VALUE = Corresponds to the answer option for the question. All questions have 4 answer options (1 to 4).</p>
  
  <p><strong>Objective</strong></p>
  
  <p>For each client and question combination, I'd like to create a separate column that stores the regression coefficient for that question regressed to Q2 in the <code>QST</code> column. </p>
  
  <p>So in the regression model, <code>Q2</code> is the dependent variable, and all other questions are the independent variables. </p>
  
  <p><strong>My attempt</strong></p>
  
  <p>My attempt is not giving me the result I want.</p>
  
  <pre><code>slopesdf = df %&gt;%
    spread(QST, VALUE, fill = 0) %&gt;%
    group_by(CLIENT) %&gt;%
    mutate(COEFFICIENT=lm(Q2 ~ .))
  </code></pre>
  
  <p>I am trying to first group by <code>CLIENT</code> &amp; <code>QST</code> and then find the slopes for each question regressed with Q2. I'm sure there's a better way of doing this.</p>
  
  <p>Currently, my attempt gives me the following error message -</p>
  
  <blockquote>
    <p>Error in mutate_impl(.data, dots) :    Evaluation error: '.' dans la
    formule et pas d'argument 'data'</p>
  </blockquote>
  
  <p><strong>Desired output</strong></p>
  
  <p>I'd like the final data frame to contain three columns: one for <code>CLIENT</code>, one for <code>QST</code> and a third called <code>COEFFICIENT</code> with the coefficients for each combination of CLIENT and QST regressed with Q2 as response variable.</p>
  
  <p>Any help on this would be greatly appreciated.</p>