Modify Data of a Data Frame with an Expression in R Programming – with() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  Name Language Age
1  abc        R  22
2  def   Python  25
3  ghi     Java  45
[1] 32 35 55
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.