Evaluate an R expression in an environment constructed from data,
possibly modifying (a copy of) the original data.
Usage
with(data, expr, ...)
within(data, expr, ...)
Arguments
data
data to use for constructing an environment. For the
default with method this may be an environment, a list, a
data frame, or an integer as in sys.call. For within,
it can be a list or a data frame.
expr
expression to evaluate.
...
arguments to be passed to future methods.
Details
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: if data is
already an environment then this is used with its existing parent.)
Note that assignments within expr take place in the constructed
environment and not in the user's workspace.
within is similar, except that it examines the environment
after the evaluation of expr and makes the corresponding
modifications to a copy of data (this may fail in the data
frame case if objects are created which cannot be stored in a data
frame), and returns it. within can be used as an alternative
to transform.
Value
For with, the value of the evaluated expr. For
within, the modified object.
See Also
evalq, attach, assign,
transform.
Examples
require(stats); require(graphics)
#examples from glm:
library(MASS)
with(anorexia, {
anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
family = gaussian)
summary(anorex.1)
})
aq <- within(airquality, { # Notice that multiple vars can be changed
lOzone <- log(Ozone)
Month <- factor(month.abb[Month])
cTemp <- round((Temp - 32) * 5/9, 1) # From Fahrenheit to Celsius
S.cT <- Solar.R / cTemp # using the newly created variable
rm(Day, Temp)
})
head(aq)
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"))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(base)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/base/with.Rd_%03d_medium.png", width=480, height=480)
> ### Name: with
> ### Title: Evaluate an Expression in a Data Environment
> ### Aliases: with with.default within within.list within.data.frame
> ### Keywords: data programming
>
> ### ** Examples
>
> require(stats); require(graphics)
> #examples from glm:
> ## No test:
> library(MASS)
> with(anorexia, {
+ anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
+ family = gaussian)
+ summary(anorex.1)
+ })
Call:
glm(formula = Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian)
Deviance Residuals:
Min 1Q Median 3Q Max
-14.1083 -4.2773 -0.5484 5.4838 15.2922
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 49.7711 13.3910 3.717 0.000410 ***
Prewt -0.5655 0.1612 -3.509 0.000803 ***
TreatCont -4.0971 1.8935 -2.164 0.033999 *
TreatFT 4.5631 2.1333 2.139 0.036035 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for gaussian family taken to be 48.69504)
Null deviance: 4525.4 on 71 degrees of freedom
Residual deviance: 3311.3 on 68 degrees of freedom
AIC: 489.97
Number of Fisher Scoring iterations: 2
> ## End(No test)
>
>
> aq <- within(airquality, { # Notice that multiple vars can be changed
+ lOzone <- log(Ozone)
+ Month <- factor(month.abb[Month])
+ cTemp <- round((Temp - 32) * 5/9, 1) # From Fahrenheit to Celsius
+ S.cT <- Solar.R / cTemp # using the newly created variable
+ rm(Day, Temp)
+ })
> head(aq)
Ozone Solar.R Wind Month S.cT cTemp lOzone
1 41 190 7.4 May 9.793814 19.4 3.713572
2 36 118 8.0 May 5.315315 22.2 3.583519
3 12 149 12.6 May 6.394850 23.3 2.484907
4 18 313 11.5 May 18.742515 16.7 2.890372
5 NA NA 14.3 May NA 13.3 NA
6 28 NA 14.9 May NA 18.9 3.332205
>
> 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))))
[[1]]
Call:
glm(formula = lot1 ~ log(u), family = Gamma)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.04008 -0.03756 -0.02637 0.02905 0.08641
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.0165544 0.0009275 -17.85 4.28e-07 ***
log(u) 0.0153431 0.0004150 36.98 2.75e-09 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Gamma family taken to be 0.002446059)
Null deviance: 3.51283 on 8 degrees of freedom
Residual deviance: 0.01673 on 7 degrees of freedom
AIC: 37.99
Number of Fisher Scoring iterations: 3
[[2]]
Call:
glm(formula = lot2 ~ log(u), family = Gamma)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.05574 -0.02925 0.01030 0.01714 0.06371
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.0239085 0.0013265 -18.02 4.00e-07 ***
log(u) 0.0235992 0.0005768 40.91 1.36e-09 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Gamma family taken to be 0.001813354)
Null deviance: 3.118557 on 8 degrees of freedom
Residual deviance: 0.012672 on 7 degrees of freedom
AIC: 27.032
Number of Fisher Scoring iterations: 3
>
> # 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"))
>
>
>
>
>
> dev.off()
null device
1
>