Reputation: 65
I tried to subset based on two conditions:
Condition 1: the SD data must be within the lower quartile of all SD data.
Condition 2: the Returns data must be within the upper quartile of all Returns data.
When I subset them separately it seemed to work fine, but when I subset them in one line I don't get what I expect.
data[data$Returns[data$SD < quantile(data$SD, .25)] > quantile(data$Returns, .75)]
For reference, the outputs look like:
quantile(data$SD, c(.25, .5, .75))
25% 50% 75%
0.6026024 0.6798148 0.9816629
quantile(data$Returns, c(.25, .5, .75))
25% 50% 75%
-2.0675 0.5200 3.3500
data[data$SD < quantile(data$SD, .25)]
Returns SD
2003-10-31 3.26 0.5721418
2004-02-29 6.88 0.5240120
2004-09-30 2.16 0.5859923
2004-12-31 0.62 0.4890445
2005-06-30 3.62 0.4798185
2005-07-31 0.28 0.5188566
2005-09-30 0.00 0.5378129
2005-11-30 4.24 0.4593336
2005-12-31 -2.39 0.4557304
data[data$Returns[data$SD < quantile(data$SD, .25)] > quantile(data$Returns, .75)]
Returns SD
2003-02-28 7.41 1.2172024
2003-05-31 7.35 1.0033962
2003-08-31 2.86 0.7264637
Any reason why or is there anything I'm doing wrong? Is it even possible to subset based on two conditions in one line?
PS why are some of my numbers in orange, but not all?
Upvotes: 0
Views: 225
Reputation: 89
You can use the "&" operator:
data[data$SD < quantile(data$SD, .25) & data$Returns > quantile(data$Returns, .75),]
Upvotes: 1