Our tutorial will introduce the with() function in R – a proper function that helps execute the command without data argument. Let’s read this tutorial now.
What is the with() function in R?
The function evaluates an expression that is constructed based on the data. It means that the function is able to execute a command with data resources relative to the original data.
Syntax:
with(data, expression, …)
Parameters:
- data: Data used to create an environment that executes the expression. The data type can be an integer, a list, or a data frame.
- expression: Command to execute operators with data from subfields (itself integer, list’s element, pandas’ columns) of the original data.
Example:
# Create two vectors named number and square number = c(1, 2, 3, 4, 5, 6, 7) square = c(1, 4, 9, 16, 25, 36, 49) # Create a data frame from the vectors myData <- data.frame(number, square) # Use with() function with the environment is myData cube = with(myData, number*square) print(cube)
Result:
1 8 27 64 125 216 343
How to use the with() function?
Advantages of the function
Maybe you will wonder why use the with() function instead of multiplying two vectors named number and square to get the same result. Take another example to see the difference.
The function returns values that depend on the expression. So, use the function whenever you want to create a customized function.
Code:
number = c(1, 2, 3, 4, 5, 6, 7) square = c(1, 4, 9, 16, 25, 36, 49) myData <- data.frame(number, square) cube = with(myData, number*square) # Create a new value for the vector number = c(1, 1, 1, 1, 1, 1, 1) # Multiply 2 vectors newVector = number*square print("The data frame is:") print(cube) print("The vector newVector is:") print(newVector)
Result:
"The data frame is:"
1 8 27 64 125 216 343
"The vector newVector is:"
1 4 9 16 25 36 49
As you can see, after changing the values of the vector number, we get a new result when multiplying two vectors, while the value of the cube does not change.
The difference is that the data of the data frame is stable; it is not affected when you change the vector. But if you do not use the with() function to calculate the result when multiplying 2 vectors of the data frame, you must determine them by the data frame’s name like that:
cube = myData$number * myData$square
Now you understand the advantage of using the with() function. We will show you examples using the function with other data types.
Use the function with an integer
Code:
myInteger = 3 # Use the function to add 3 to the integer cat(paste("The integer after adding:", (with(myInteger, myInteger + 3))))
Result:
The integer after adding: 6
Use the function with a list
Code:
myList = list(2, "String", 22.6, TRUE) # Use the function to pick up the second element cat(paste("The second element of the list is:", with(myList, (myList[2]))))
Result:
The second element of the list is: String
Discussion
If you are familiar with the Python programming language, you will see the similarity between the with() function and the lambda() function in Python.
In the above examples, when working with an integer, you can use the lambda() function like that
x = lambda myInteger: myInteger + 3 print(x(3))
Or working with a list.
myList = list((2, "String", 22.6, True)) x = lambda list:list[1] print(x(myList))
Summary
In summary, the with() function in R is handy when working a data frame without typing the names of columns. In some cases, the function makes your code clearer and shorter. Because of the easy syntax and effective result, we recommend using the with() function when working with data having many parameters.
Maybe you are interested:

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP