[R] building formula objects

Thomas Lumley tlumley at u.washington.edu
Wed Jul 10 01:57:38 CEST 2002


On 9 Jul 2002, Russell Senior wrote:

<snip>
> Then I use sapply, as in:
>
>   > t(sapply(raymonds,russell)) -> tmp
>
> If I coerce the individual "rows" in the result to data frames, e.g.:
>
>   > as.data.frame(tmp[1,])
>      Df    Sum.Sq   Mean.Sq  F.value       Pr..F.
>   1   4  161.3588 40.339705 23.10912 2.988881e-18
>     955 1667.0654  1.745618       NA           NA
>
> and even:
>
>   > apply(tmp,1,as.data.frame)
>   $twa
>      Df    Sum.Sq   Mean.Sq  F.value       Pr..F.
>   1   4  161.3588 40.339705 23.10912 2.988881e-18
>     955 1667.0654  1.745618       NA           NA
>
>   $twa.bh
>      Df     Sum.Sq   Mean.Sq F.value       Pr..F.
>   1   4   3.577902 0.8944754 6.57847 3.186600e-05
>     955 129.851484 0.1359701      NA           NA
>
<snip>
> I get something very close to what I want, but now I want to collect
> these tmp rows into a single data frame that I can cleanly pass to
> write.table().  Something like:
>
>      Df    Sum.Sq   Mean.Sq  F.value       Pr..F.
>   1   4  161.3588 40.339705 23.10912 2.988881e-18
>     955 1667.0654  1.745618       NA           NA
>   1   4   3.577902 0.8944754 6.57847 3.186600e-05
>     955 129.851484 0.1359701      NA           NA
>   1   4    497.49 124.3725 0.7928758 0.5298613
>     955 149803.70 156.8625        NA        NA
>   1   4    20685.82  5171.456 0.1604102 0.9582729
>     955 30788197.64 32238.950        NA        NA

This is what I regard as the canonical example of do.call()

If these were separate variables instead of elements of tmp you could do
eg
  rbind(twa, twa.bh, rcm, rcm, s25, s5)
But they aren't, so
  do.call("rbind", tmp)
does the trick.  This creates a call to rbind() where the arguments are
the elements of tmp.

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list