1

Say I have the following data:

         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
   [1,]  983  362  170  303  914  843  480  489  474   355
   [2,]  204  205  810  762  758  404   24  111  265   586

Each of these values corresponds to an index in another set called labels. I would like to create the exact same structure as above, but instead of having the index, I want the value.

So far I have tried:

labels[attr(k,"nn.index")[1,]]

which returns [1] 2 2 2 2 2 2 2 2 2 2, my desired result for the first row, and:

labels[attr(k,"nn.index")[2,]]

which returns [1] 0 0 0 0 0 0 0 0 0 0, my desired result for the second row.

My problem is that if I try to do this for all rows I get the following results:

labels[attr(k,"nn.index")[1:2,]]
 [1] 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0

What would I have to change to get the result below?

         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
   [1,]   2    2    2    2    2    2    2    2    2    2
   [2,]   0    0    0    0    0    0    0    0    0    0

Also, in real life there are thousands of rows, not just two.

nograpes
  • 18,623
  • 1
  • 44
  • 67
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

1 Answers1

3

Do:

apply(attr(k,"nn.index"), 2, function(col) labels[col])

But didn't I give you this precise answer in your previous question? (See the closest.labels variable).

Community
  • 1
  • 1
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • You are right you did. Sorry, I didn't make the connection until just now. – Abe Miessler Aug 29 '12 at 05:46
  • 2
    That's all right- cheers. A friendly word to the wise- I couldn't help but notice you've asked a lot of questions about every step of this project. You're a top contributor in several other tags: my guess is that if a newcomer in C# asked questions this densely, you'd suggest that if they spent more time trying to solve and understand the problem themselves, they'd learn the language faster. Again- just friendly advice! – David Robinson Aug 29 '12 at 05:52
  • You make an excellent point. I'll try to do a little more research next time. Been a while since I started from scratch with a new language. – Abe Miessler Aug 29 '12 at 06:00