You are currently viewing R with() and within() Functions

What are with() and within() functions in R and how to use them? These two functions take the DataFrame and expression as input and evaluate the expression with the data from the DataFrame.

The with() function returns the vector with the result of the expression and within() returns a new DataFrame either by adding or updating a column with the result of the expression.

  • with() – Evaluates the expression without altering the original data frame.
  • within() – Evaluates the expression and creates a copy of the original data frame.

1. Quick Examples

Following are quick examples of with() and within() functions.


# Quick examples

# use of with()
with(df, X * Y)

# add as new column
df$mult = with(df, X * Y)
df

# Using within()
df2 <- within(df, sub <- Y-X)
df2

2. with() in R

The with() in R is a base function that works with the DataFrame. This function takes the DataFrame and the expression as input and returns the Vector. The expression uses the data from the DataFrame to evaluate.

2.1 Syntax of with()

The following is the syntax of the with() function.


# Syntax
with(dataframe, expression)

2.2 R with() Example

By using with() function you can execute the expression with the data from the DataFrame. In the below example I am multiplying columns X and Y. This function returns the result of the expression as an R Vector.

The with() function does not update the original data frame.


# Create DataFrame
df <- data.frame(X = c(2, 4, 3),     
                 Y = c(3, 5, 2))
df   

# Use with()
with(df, X * Y)

Yields below output.

with in R

2.3 Result of with() Add as a New Column

Since the with() returns a vector, you can easily add it as a column to DataFrame.


# Assign it as a new column
df$mult = with(df, X * Y)
df

3. within() in R

The within() function in R is used to add the evaluated expression as a new column to the DataFrame and returns the new DataFrame with the new column. As you learned above, the with() function doesn’t change the DataFrame in any way. The within() function also doesn’t update the existing DataFrmae instead it returns a new DataFrame with a new column

3.1 Syntax of within()

The following is the syntax of the with() function.


# Syntax
within(dataframe, new-column <- expression)

3.2 R within() Example

The within() function returns a new DataFrame with the added column from the expression result. By using the existing column, you can also update the existing column with the expression result.

The following example adds a new column sub to the DataFrame.


df <- data.frame(X = c(2, 4, 3),     
                 Y = c(6, 8, 9))
df  

# Using within()
df2 <- within(df, sub <- Y-X)
df2

Yields below output.

within in R

4. Conclusion

In this article, you have learned the syntax of with() and within() base functions in R and learned how to use them with examples. The with() function returns the vector with the result of the expression and within() returns a new DataFrame either by adding or updating a column with the result of the expression.

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium