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

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago