In R I have 20 different datasets which all contain the same variables. data1999, data2000, ... data2018
.
The data contains stocks with some ratios. Right now I want to sort these stocks based on 2 criteria.
First I want to take the first 2 deciles of a certain variable which is called TVDecile
, of this cheapest 2 deciles, I want to select the 30 stocks which rank the best based on the variable Momentum
.
So for example:
data199$TVDecile <- c(1,8,3,5,6,2)
data1999$momentum <- c(0.2 , -0.5, 0.4, 0.1, 0.6, 0.3)
What I then need to do is first filter on data1999$TVDecile
. I only need the first 2 deciles (so numbers 1 and 2). I could maybe do this with an If statement?
After that I want to sort the remaining numbers by momentum. So the one with the momentum of 0.3 should come first in the list and the one with the momentum of 0.2 should come second. A new variable should be created which contains these 2 values.