Don’t miss this article if you want to use the transform() function in R. Please read the entire article; you will get a lot of helpful information and can use this function efficiently.
Transform() in R
We can use the transform()
function to modify data columns or add new columns in a data frame quickly and easily.
The syntax of transform():
transform(dataframe, val)
Parameters:
dataframe:
The data frame is used modified.val:
The new value is modified for data frame.
Using transform() to modify column
If you want to know how to use the transform() function to modify a column, follow the code below:
First, we need a data frame example as follows:
dataFrame <- data.frame(names = c("Anastasia Schaefer", "Shyann Barrows", "Candida Prosacco", "Marcos Swift", "Kassandra Watsica", "Angie Botsford"), math = c(9, 8, 6, 7, 8, 9), physic = c(10, 9, 10, 8, 8, 7), chemistry = c(9, 8, 10, 7, 9, 8)) # View data frame dataFrame
Output:
names math physic chemistry
1 Anastasia Schaefer 9 10 9
2 Shyann Barrows 8 9 8
3 Candida Prosacco 6 10 10
4 Marcos Swift 7 8 7
5 Kassandra Watsica 8 8 9
6 Angie Botsford 9 7 8
Then, we will modify math column by using transform() function.
dataFrame <- data.frame(names = c("Anastasia Schaefer", "Shyann Barrows", "Candida Prosacco", "Marcos Swift", "Kassandra Watsica", "Angie Botsford"), math = c(9, 8, 6, 7, 8, 9), physic = c(10, 9, 10, 8, 8, 7), chemistry = c(9, 8, 10, 7, 9, 8)) attach(dataFrame) # Add math column by 10 and divide by 2 newdataFrame <- transform(dataFrame, math = (math + 10) / 2) # View new data frame newdataFrame
Output:
names math physic chemistry
1 Anastasia Schaefer 9.5 10 9
2 Shyann Barrows 9.0 9 8
3 Candida Prosacco 8.0 10 10
4 Marcos Swift 8.5 8 7
5 Kassandra Watsica 9.0 8 9
6 Angie Botsford 9.5 7 8
Using transform() to create a new column
We will create a new medium column to calculate the average score by taking (math + physic + chemistry) / 3. See the code below to understand more:
dataFrame <- data.frame(names = c("Anastasia Schaefer", "Shyann Barrows", "Candida Prosacco", "Marcos Swift", "Kassandra Watsica", "Angie Botsford"), math = c(9, 8, 6, 7, 8, 9), physic = c(10, 9, 10, 8, 8, 7), chemistry = c(9, 8, 10, 7, 9, 8)) attach(dataFrame) # Create a medium column newdataFrame <- transform(dataFrame, medium = round((math + physic + chemistry) / 3)) # View new data frame newdataFrame
Output:
names math physic chemistry medium
1 Anastasia Schaefer 9 10 9 9
2 Shyann Barrows 8 9 8 8
3 Candida Prosacco 6 10 10 9
4 Marcos Swift 7 8 7 7
5 Kassandra Watsica 8 8 9 8
6 Angie Botsford 9 7 8 8
You can create many columns, for example:
dataFrame <- data.frame(names = c("Anastasia Schaefer", "Shyann Barrows", "Candida Prosacco", "Marcos Swift", "Kassandra Watsica", "Angie Botsford"), math = c(9, 8, 6, 7, 8, 9), physic = c(10, 9, 10, 8, 8, 7), chemistry = c(9, 8, 10, 7, 9, 8)) attach(dataFrame) # Create many columns newdataFrame <- transform(dataFrame, medium = round((math + physic + chemistry) / 3), english = c(9, 8, 6, 9, 6, 10)) # View new data frame newdataFrame
Output:
names math physic chemistry medium english
1 Anastasia Schaefer 9 10 9 9 9
2 Shyann Barrows 8 9 8 8 8
3 Candida Prosacco 6 10 10 9 6
4 Marcos Swift 7 8 7 7 9
5 Kassandra Watsica 8 8 9 8 6
6 Angie Botsford 9 7 8 8 10
Summary
I hope this article has brought you a lot of helpful knowledge. If you have any questions, please leave a comment below. I will answer as possible. Thanks for reading!
Maybe you are interested:
- Names in R: How To Use The Names() Function in R?
- Spread() Function In R: How To Use spread() In R
- Round() function in R: How To Use Round Function In R

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript