[R] Subsetting/modifying a symbolic formula
Bryan Hanson
hanson at depauw.edu
Wed Oct 21 19:37:21 CEST 2009
Thanks Gabor, you taught me two useful things: all.vars, and the fact that a
formula object always has length 3. Problem solved. Bryan
On 10/21/09 11:57 AM, "Gabor Grothendieck" <ggrothendieck at gmail.com> wrote:
> Here is one way:
>
>> fo <- y ~ f1 * f2
>> one.x <- lapply(all.vars(fo[[3]]), function(x) { fo[[3]] <- as.name(x); fo })
>> one.x
> [[1]]
> y ~ f1
>
> [[2]]
> y ~ f2
>
>
> On Wed, Oct 21, 2009 at 11:29 AM, Bryan Hanson <hanson at depauw.edu> wrote:
>> Hello All..
>>
>> Please consider the following:
>>
>> y <- rnorm(20, mean = 10)
>> f1 <- as.factor(rep(c("A", "B", "B", "A"), 5))
>> f2 <- as.factor(rep(c("C", "D"), 10))
>> testdata <- data.frame(y, f1, f2)
>>
>> testFunc <- function(formula, data, ...) {
>> # mf <- model.frame(formula, data)
>> kw.res <- kruskal.test(formula, data)
>> }
>>
>> res <- testFunc(y ~ f1 * f2, testdata)
>>
>> Which works perfectly well. Now, I would like to do some other tests which
>> allow only one factor, for instance wilcox.test. So, I would like to modify
>> my formula so that it reads y ~ f1 or y ~ f2, then conduct more tests. What
>> is the smart way to subset or modify an existing symbolic formula? I have
>> been staring at the attributes of res and mf (commented out currently) but
>> these are nested in a way I find difficult to extract. ?terms,
>> ?terms.formula, ?formula etc all discuss these attributes but as I said, it
>> seems a bit impenetrable. RSiteSearch("symbolic formula") returns too many
>> answers. No doubt I am missing the obvious, as this is my first foray into
>> using formulas in my own functions.
>>
>> TIA, Bryan
>> *************
>> Bryan Hanson
>> Acting Chair
>> Professor of Chemistry & Biochemistry
>> DePauw University, Greencastle IN USA
>>
>>
>>> sessionInfo()
>> R version 2.10.0 RC (2009-10-19 r50172)
>> i386-apple-darwin8.11.1
>>
>> locale:
>> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>>
>> attached base packages:
>> [1] datasets tools grid utils stats graphics grDevices
>> methods
>> [9] base
>>
>> other attached packages:
>> [1] ggplot2_0.8.3 reshape_0.8.3 proto_0.3-8 mvbutils_22.0
>> [5] ChemoSpec_1.2 lattice_0.17-25 mvoutlier_1.4 plyr_0.1.8
>> [9] RColorBrewer_1.0-2 chemometrics_0.4 som_0.3-4
>> robustbase_0.4-5
>> [13] rpart_3.1-45 pls_2.1-0 pcaPP_1.7 mvtnorm_0.9-7
>> [17] nnet_7.2-48 mclust_3.2 MASS_7.2-48 lars_0.9-7
>> [21] e1071_1.5-19 class_7.2-48
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
More information about the R-help
mailing list