R Programming

Using with() function to simplify your work in R

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

with() function in R provides an alternative and often easier way to deal with data management and analysis. Say we have a data frame ‘grade’, and want to calculate the summary statistics of variable ‘Math’ and correlation between variables ‘Math’ and ‘Physics’. In the normal way, we can write the code as follows:

#to show first three observations in data frame 'grade'
head(grade,3)
  StudentID        Fullname Race Gender Country Age Math Physics
1         1     James Zhang    A   Male      US  23   73      70
2         2       Wilson Li    E Female      UK  26   95      76
3         3 Richard Nuan Ye    A   Male      UK  35   77      83
  Chemistry       Date
1        87 10/31/2008
2        83  3/16/2008
3        92  5/22/2008

#summary statistics of variable 'Math' of data frame 'grade'
#output
summary(grade$Math)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   60.0    72.5    79.0    78.9    87.0    95.0 

#correlation between variables Math and Physics of data frame grade
cor(grade$Math,grade$Physics)
#output
[1] -0.04852046

If there are only two lines of code like this, it is OK. But say if we have 5 or 10 of lines of codes for correlations and summary statistics for different variables in the same data frame, using with() can save most of the repeated work.

#Using with() to conduct same job as above
with(grade, {
  print(summary(Math))
  print(cor(Math, Physics))
})
#output
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   60.0    72.5    79.0    78.9    87.0    95.0 
[1] -0.04852046

Similar as R function, the variable defined inside a with() block exists only in the block. If we refer to this variable outside of the with() block, an error will be incurred.

#local variable assignments exist only within the function brackets
with(grade, {
   test <- summary(Math)
   test
 })
#output
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   60.0    72.5    79.0    78.9    87.0    95.0 
#Does not work if we refer to a local object outside of the with block
test
#output
Error: object 'test' not found

One way to bypass this restriction is using double-arrow assignment symbol in the with() block.

#using double-arrow assignment symbol inside with() block
with(grade, {
   print(summary(Math))
   print(cor(Math,Physics))
   test<<- summary(Math)
+ })
#output
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   60.0    72.5    79.0    78.9    87.0    95.0 
[1] -0.04852046
#refer to object outside with() block
test
#output
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   60.0    72.5    79.0    78.9    87.0    95.0 

You can also watch R tutorial videos on our YouTube channel.

wilsonzhang746

Recent Posts

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

5 days ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

2 weeks ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

1 month ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

1 month ago

Download R Course source files

Click here to download R Course source files !

10 months ago

Download Python Course source files

Click here to download Python Course Source Files !

10 months ago