Modify values of a Data Frame in R Language – transform() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)                                          
                                     

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.