with evaluates an expression in an environment constructed from data, possibly modifying the data in the process. within is similar to with, except that it modifies the data frame directly and only examines the environemnt after the evaluation of the expression.
Case study 1: to create a new variable new_var in df that is the sum of var1 and var2
In the code chunk above, two new columns are being created and please note that no column has been added to the original data. You have to assign the output from the within environment to a new object.
Citation
BibTeX citation:
@online{tan2024,
author = {Tan, Joan},
title = {`With` Vs `Within`},
date = {2024-01-16},
url = {https://joantan.org/R/2024-01-16-with-within/},
langid = {en}
}