# [R] building formula objects

Russell Senior seniorr at aracnet.com
Wed Jul 10 01:43:45 CEST 2002

```>>>>> "Bill" == Bill Venables <Bill.Venables at cmis.csiro.au> writes:

Bill> [WNV] [...] See the Programmer's Niche article in the latest
Bill> edition of R news for a general strategy for handling this kind
Bill> of problem (at the risk of touting my own stuff).

The goal of my exercise it to create a nice blob of data that I can
export, ultimately, into a document table.

I've got a data frame called pe.ours which contains a bunch of
response variables kept in a list, raymonds:

> raymonds
[1] "twa"    "twa.bh" "rcm"    "rcm2"   "s25"    "s5"     "l4"     "l16"
[9] "e4"     "e16"    "p90"    "p95"    "p99"    "max"

For this go-round, I am just looking at the factor, my.factor, (also a
member of pe.ours), so my function is:

russell <- function(n) {
tAsgn <- paste("tList[[n]] <- try(anova(lm(X ~ my.factor, data=pe.ours)))",
sep="")
tAsgn <- parse(text = tAsgn)[[1]]
TAsgn <- do.call("substitute", list(tAsgn, list(X = as.name(n))))
eval(TAsgn)
}

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]

\$p99
Df    Sum.Sq  Mean.Sq   F.value    Pr..F.
1   4    497.49 124.3725 0.7928758 0.5298613
955 149803.70 156.8625        NA        NA

\$max
Df      Sum.Sq   Mean.Sq   F.value    Pr..F.
1   4    20685.82  5171.456 0.1604102 0.9582729
955 30788197.64 32238.950        NA        NA

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

A bonus would be to retain the rownames somehow.

I am just not yet "getting" R's multi-dimensional data objects and how
to manipulate them.

--
Russell Senior         ``The two chiefs turned to each other.
seniorr at aracnet.com      Bellison uncorked a flood of horrible
profanity, which, translated meant, `This is
extremely unusual.' ''
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```