with() function in R provides an alternative way of carrying out several operations bypassing inputting object name repeatedly. For example, we have a data frame ‘grade’ on hand, and want to calculate the correlation coefficient between Math and Physics variables and show summary statistics of numerical variables, we can write code as follows:
#show first observations of data frame
head(grade)
#output
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
4 4 Mary Deng E Female US 21 60 99
5 5 Jason Wilson A Male UK 19 77 89
6 6 Jennifer Hopkin A Female UK 43 79 64
Chemistry Date
1 87 10/31/2008
2 83 3/16/2008
3 92 5/22/2008
4 84 1/24/2009
5 93 7/30/2009
6 83 4/5/2009
#show summary statistics of Math variable
summary(grade$Math)
#result
Min. 1st Qu. Median Mean 3rd Qu. Max.
60.0 72.5 79.0 78.9 87.0 95.0
#correlation between Math and Physics grades
> cor(grade$Math,grade$Physics)
#result
[1] -0.04852046
If we use with, all the relevant codes will be packed into a with() block, and object name ‘grade’ are therefore ommited.
#Using with block to do same operations
with(grade, {
print(summary(Math))
print(cor(Math, Physics))
})
#result
Min. 1st Qu. Median Mean 3rd Qu. Max.
60.0 72.5 79.0 78.9 87.0 95.0
[1] -0.04852046
Just note the objects created inside the with() block usually exist only in it. So if you want to call this object outside of with() block, it does not exist.
#object assignments exist only within the function brackets
with(grade, {
test <- summary(Math)
test
})
#not work if we refer to the object created inside with()
test
#output
Error: object 'test' not found
If you want to bypass this limit, you can use instead a double arrow assignment symbol inside the with() block.
#use special assignment operator (<<-) to bypass the local scope limit
with(grade, {
summary(Math)
cor(Math,Physics)
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
#Then we can use same technique for updating grade data frame
with(grade, {
grade$totalscore <- Math + Physics + Chemistry
print(grade$totalscore)
grade <<- grade
})
#show first observations of data frame, and it works
head(grade)
#output
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
4 4 Mary Deng E Female US 21 60 99
5 5 Jason Wilson A Male UK 19 77 89
6 6 Jennifer Hopkin A Female UK 43 79 64
Chemistry Date totalscore
1 87 10/31/2008 230
2 83 3/16/2008 254
3 92 5/22/2008 252
4 84 1/24/2009 243
5 93 7/30/2009 259
6 83 4/5/2009 226
0 Comments