1

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...

Stuart Allen
  • 1,537
  • 1
  • 10
  • 19
Jared Marks
  • 948
  • 8
  • 15
  • It would be good to start with data and show your code for both situations. As you mention, you are missing something and the thing you are missing is most likely a bug in your code that you haven't seen. Currently, you are asking someone else to debug your code without seeing it first. – Harlan Nelson Dec 17 '17 at 00:42
  • This is all the code that is needed to reproduce the error. – Jared Marks Dec 17 '17 at 00:48
  • @JaredMarks It's courteous to provide all code needed to reproduce the error. In this case that would include the code to create the matrices. This just makes it quicker and easier for people to help you. – Stuart Allen Dec 17 '17 at 01:26

1 Answers1

2

Use colnames() and rownames() rather than names() when referring to a matrix:

mean.momentum.1 <- mean(result.1)

colnames(result.1)[which(result.1 > mean.momentum.1)]
## [1] "TLT" "GLD"

Also note the syntax of colnames() above. You are returning a subset of the vector colnames(result1) using which() to select the elements of the vector for which result.1 is greater than mean.momentum.1.

This works fine for result.2 also:

mean.momentum.2 <- mean(result.2)

colnames(result.2)[which(result.2 > mean.momentum.2)]
## [1] "TLT"
Stuart Allen
  • 1,537
  • 1
  • 10
  • 19
  • 1
    Thank you so much, I forgot about using the which() function workaround. Any idea why it worked fine in general but not in that instance? Sorry for not including the code. – Jared Marks Dec 17 '17 at 02:08
  • `names()` does not really "work" for matrices. It won't give an error, and it will return a result, but cannot be relied upon. In your first case the result of `names()` happened to be what you expected. To be honest, I don't really understand what is going on, but there's some discussion [here](https://stackoverflow.com/questions/24799153/what-is-the-difference-between-names-and-colnames) that may be of interest. – Stuart Allen Dec 17 '17 at 02:24