Let’s introduce risk free assets in our portfolio. Risk free assets are those which have zero risk and very small return like govt. bond.

We will create a portfolio consisting both of risky and risk free assets:

Modifying the weight function that we have created in portfolio optimizationI post:

Using this function for our basket of assets:
Here, weights for 5 risky and 1 risk free assets are getting displayed. It includes “short” position stocks as well.

Now using portfolio.optim function present in “tseries” package (“shorts” are allowed):
Above 5 weights are for risky assets and sum NOT= 1. Therefore weight of riskfree asset = 10.4242=0.5758

When “shorts” are not allowed:
Above 5 weights are for risky assets and sum NOT= 1. Therefore weight of riskfree asset = 15909=0.4091