# with & within Functions in R (Example)

This tutorial explains how to use the with() and within() functions in the R programming language.

## Example Data

my_data <- data.frame(x = c(2, 4, 6, 8), # Create example data y = c(3, 1, 4, 1)) |

my_data <- data.frame(x = c(2, 4, 6, 8), # Create example data y = c(3, 1, 4, 1))

## with Function in R

The with function evaluates an expression within the data environment and returns the output to the RStudio console.

with(my_data, x + y) # Application of with function # 5 5 10 9 |

with(my_data, x + y) # Application of with function # 5 5 10 9

## within Function in R

The within function evaluates an expression and concatenates the result of this expression to the original data frame:

within(my_data, z <- x + y) # Application of within function # x y z # 2 3 5 # 4 1 5 # 6 4 10 # 8 1 9 |

within(my_data, z <- x + y) # Application of within function # x y z # 2 3 5 # 4 1 5 # 6 4 10 # 8 1 9