This one is really stumping me. I cannot find what is wrong with this line of code and I'm using it in a function and it makes the entire function fail.
Here is a vector of class 'matrix', result.1
:
SPY TLT VGK EEM BIV RWX IYR EWJ DBC GLD PCY LQD TIP
-0.08 0.09 -0.07 -0.07 -0.09 -0.07 -0.07 -0.07 -0.066 -0.04 -0.08 -0.08 -0.08
I calculate the mean of this vector by running this line of code:
mean.momentum.1 <- mean(result.1)
The result is a numeric with a value of -0.062309. I want to get the names of the columns that are greater than the mean, so I run this code:
names(result.1[,(result.1 > mean.momentum.1)])
The output is as I expect, returning a character vector "TLT", "GLD".
Here is the issue: when I do this with a second, nearly identical matrix result.2
I get a NULL
result every time, when I should be getting the result "TLT".
Here is the matrix result.2
:
SPY TLT VGK EEM BIV RWX IYR EWJ DBC GLD TIP PCY LQD
-0.08 0.15 -0.07 -0.06 -0.054 -0.07 -0.07 -0.07 -0.06 -0.06 -0.07 -0.07 -0.06
I calculate the mean using the same method as above (-0.05298) and name it mean.momentum.2
Then, I run this line:
names(result.2[,(result.2 > mean.momentum.2)])
I get NULL
back every time, when I expect "TLT". What is going wrong with the way I am subsetting? If I run the line:
result.2 > mean.momentum.2
I get a logical vector where TLT is TRUE
, but it does not work when I try to subset with this method. It works perfectly in the first instance, but never works in the second instance, and since I get NULL
back, my entire function fails.
Thank you...