First of all I would like to note that I am not trying to get someone to do the work for me. This is intended to be a feasibility question (i.e. I would like to know if it can be done and if so, where can I find more information on how to achieve it).

So, here's what I would like to achieve (btw. I am using RStudio):

I would like to calculate returns of different portfolios formed based on past returns.

And here are my issues:

- Shares are not included for the entire time frame, therefore there are a lot of #N/A values. If there's an error in one field, R is supposed to ignore the entire column.

- Next, the amount of shares included varies in time, so I would like to know if it is possible to tell R to count the amount included and form equally sized groups/portfolios (or as equal as possible since for example 158 can not be divided by 5 or 10).

- How can I tell R to filter (ascending or descending) for example row D based on (row A + row B)?

I hardly doubt this can be achieved, but the alternative would be doing it manually in excel and that can hardly be the way it's supposed to be done...:/

I would be very glad if anyone could guide/help me!

Many thanks in advance!