withAs: Alternative with() function with explicit naming

View source: R/misc.R

withAsR Documentation

Alternative with() function with explicit naming

Description

An alternative to the with function in base R where data is represented by a temporary variable within a new environment rather than evaluating expr directly within data. This can help explicitly show which variables are contained within data from those outside.

Usage

withAs(..., expr = NULL)

Arguments

...

data to be used in expression. Argument names are used as temporary variable names. So xx=data.frame('a'=1:10) will result in a data.frame called xx with column a in the evaluating environment.

expr

expression to evaluate. if expr is not defined by name then the last unnamed argument is used.

Value

the value of the evaluated expr

Examples

d<-3:12
longNameDataFrame<-data.frame('a'=1:10,'b'=2:11)
with(longNameDataFrame,a+b+d)
withAs(xx=longNameDataFrame,xx$a+xx$b+d)
anotherDf<-data.frame('c'=3:12)
withAs(xx=longNameDataFrame,zz=anotherDf$c*10,xx$a+xx$b+zz)

sherrillmix/dnar documentation built on July 18, 2022, 10:07 p.m.