with                  package:base                  R Documentation

_E_v_a_l_u_a_t_e _a_n _E_x_p_r_e_s_s_i_o_n _i_n _a _D_a_t_a _E_n_v_i_r_o_n_m_e_n_t

_D_e_s_c_r_i_p_t_i_o_n:

     Evaluate an R expression in an environment constructed from data.

_U_s_a_g_e:

     with(data, expr, ...)

_A_r_g_u_m_e_n_t_s:

    data: data to use for constructing an environment. For the default
          method this may be an environment, a list, a data frame, or
          an integer as in 'sys.call'.

    expr: expression to evaluate.

     ...: arguments to be passed to future methods.

_D_e_t_a_i_l_s:

     'with' is a generic function that evaluates 'expr' in a local
     environment constructed from 'data'.  The environment has the
     caller's environment as its parent.  This is useful for
     simplifying calls to modeling functions.

     Note that assignments within 'expr' take place in the constructed
     environment and not in the user's workspace.

_S_e_e _A_l_s_o:

     'evalq', 'attach'.

_E_x_a_m_p_l_e_s:

     require(stats); require(graphics)
     #examples from glm:
     ## Not run: 
     library(MASS)
     with(anorexia, {
         anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                         family = gaussian)
         summary(anorex.1)
     })
     ## End(Not run)

     with(data.frame(u = c(5,10,15,20,30,40,60,80,100),
                     lot1 = c(118,58,42,35,27,25,21,19,18),
                     lot2 = c(69,35,26,21,18,16,13,12,12)),
         list(summary(glm(lot1 ~ log(u), family = Gamma)),
              summary(glm(lot2 ~ log(u), family = Gamma))))

     # example from boxplot:
     with(ToothGrowth, {
         boxplot(len ~ dose, boxwex = 0.25, at = 1:3 - 0.2,
                 subset = (supp == "VC"), col = "yellow",
                 main = "Guinea Pigs' Tooth Growth",
                 xlab = "Vitamin C dose mg",
                 ylab = "tooth length", ylim = c(0,35))
         boxplot(len ~ dose, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2,
                 subset = supp == "OJ", col = "orange")
         legend(2, 9, c("Ascorbic acid", "Orange juice"),
                fill = c("yellow", "orange"))
     })

     # alternate form that avoids subset argument:
     with(subset(ToothGrowth, supp == "VC"),
          boxplot(len ~ dose, boxwex = 0.25, at = 1:3 - 0.2,
                  col = "yellow", main = "Guinea Pigs' Tooth Growth",
                  xlab = "Vitamin C dose mg",
                  ylab = "tooth length", ylim = c(0,35)))
     with(subset(ToothGrowth,  supp == "OJ"),
          boxplot(len ~ dose, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2,
                  col = "orange"))
     legend(2, 9, c("Ascorbic acid", "Orange juice"),
            fill = c("yellow", "orange"))

