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
data < - data.frame(x1 = c( 1 , 2 , 3 , 4 ),
x2 = c( 5 , 6 , 7 , 8 ))
data_ex1 < - transform(data, x1 = x1 + 10 )
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
data < - data.frame(x1 = c( 11 , 12 , 13 , 14 ),
x2 = c( 5 , 6 , 7 , 8 ))
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.
Last Updated :
26 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...