Related Articles
Modify values of a Data Frame in R Language – transform() Function
• Last Updated : 26 May, 2020

`transform()` function in R Language is used to modify data. It converts the first argument to the data frame.
This function is used to transform/modify the data frame in a quick and easy way.

Syntax:
transform(data, value)

Parameters:
data: Data Frame to be modified
value: new modified value of data

Example 1: Use transform() on variables

 `# R program to illustrate ` `# transform on variables ` ` `  `# Create example data frame ` `data <``-` `data.frame(x1 ``=` `c(``1``, ``2``, ``3``, ``4``),             ` `                   ``x2 ``=` `c(``5``, ``6``, ``7``, ``8``)) ` ` `  ` ``# Apply transform function ` `data_ex1 <``-` `transform(data, x1 ``=` `x1 ``+` `10``)   ` ` `  `# Print data       ` `print``(data_ex1)                                           ` `                                    `

Output:

```x1= 11, 12, 13, 14.
x2 = 5, 6, 7, 8.```

Here in the above code, we have a data framework and we transform it by adding 10(x1 = x1 + 10) to each of the values.
The values of x2 will not get changed as we do not apply any transformation to it.

Example 2: Add new variables to data

 `# R program to illustrate  ` `# Adding new variables to data ` ` `  `data <``-` `data.frame(x1 ``=` `c(``11``, ``12``, ``13``, ``14``),             ` `                   ``x2 ``=` `c(``5``, ``6``, ``7``, ``8``)) ` ` `  `# Apply transform function ` `data_ex2 <``-` `transform(data, x3 ``=` `c(``1``, ``3``, ``3``, ``4``))   ` ` `  `print``(data_ex2)  `

Output:

```X1= 11, 12, 13, 14.
X2 = 5, 6, 7, 8.
X3 = 1, 3, 3, 4.
```

Here in the above code, we are adding a new variable x3 to the previous data.

My Personal Notes arrow_drop_up
Recommended Articles
Page :