Extract degrees of freedom in R
<p>I am running a large number of linear regressions, and for each regression I would like to save the adjusted R squared and the degrees of freedom each in a seperate file.</p>

<p>The code below does this perfectly for the adjusted R squared, and I can add the value name of the list to the file (so I can identify to which subset of mydata the R squared belongs). In sum, it gives me 1 column with the list value, and 1 column with the R².</p>

<p>For the degrees of freedom, R gives a 3-vector (p, n-p, p*) by default. Adding the row.name=i statement does not work here. I would only need n-p. Is there a way to save the middle figure from the vector only and add the row name to the saved file?</p>

<p>I created a reproducible example below:</p>

<p>Fictive dataset (mydata)</p>

<pre><code>v1  v2  v3

4   9   1
5   78  1
6   11  1
8   12  2
10  13  2
11  65  2
15  34  2
</code></pre>

<p>Code:</p>

<pre><code>list&lt;-unique(mydata$v3)
for (i in 1:length(list)){


  newdata &lt;- mydata[ which(mydata$v3==i),]

  reg &lt;- lm((v1~v2), data=newdata)
  write.table(summary(reg)$adj.r.square, file="rsquared.txt", append=TRUE, col.names = F, row.names=i)
  write.table(summary(reg)$df, file="degreesoffreedom.txt", append=TRUE, col.names = F)


  rm(reg)
  rm(newdata)

}
</code></pre>

<p>What I currently get for the file "degreesoffreedom.tx"</p>

<pre><code>"1" 2
"2" 1
"3" 2
"1" 2
"2" 2
"3" 2
</code></pre>

<p>What I need is:</p>

<pre><code>"1" 1
"2" 2
</code></pre>