with()
function in R Language is used to modify data of a data frame by evaluating an expression within the arguments of the function.
Syntax: with(x, expr)
Parameters:
x: Data frame
expr: Expression to modify data
Example 1:
# R program to modify data of an object # Calling predefined data set BOD # Calling with() function with(BOD, {BOD$demand < - BOD$demand + 1 ; print (BOD$demand)})
|
Output:
Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 [1] 9.3 11.3 20.0 17.0 16.6 20.8
Example 2:
# R program to modify data of an object # Creating a data frame df = data.frame(
"Name" = c( "abc" , "def" , "ghi" ),
"Language" = c( "R" , "Python" , "Java" ),
"Age" = c( 22 , 25 , 45 )
) df # Calling with() function with(df, {df$Age < - df$Age + 10 ; print (df$Age)})
|
Output:
Name Language Age 1 abc R 22 2 def Python 25 3 ghi Java 45 [1] 32 35 55
Article Tags :