Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:17:33+00:00 2026-06-10T10:17:33+00:00

I have the following code with uses the FNN Package : k <- knn(train,

  • 0

I have the following code with uses the FNN Package:

k <- knn(train, test, labels, k = 10, algorithm="cover_tree")

I am able to look at the data returned by the call to knn using the code below:

> attr(k, "nn.dist")[1,]
 [1] 1385.398 1687.901 1722.884 1846.694 1978.250 1998.157 2003.518 2004.494 2054.382 2059.128
> attr(k, "nn.index")[1,]
 [1] 983 362 170 303 914 843 480 489 474 355

What I would like to do is modify the values that are in nn.dist based on the values that are in nn.index.

I know this probably doesn’t make sense, but for the sake of argument, how would I go about setting each value in nn.dist to nn.dist * nn.index/1000?

So attr(k, "nn.dist")[1,1] would go from 1385.398 to 1385.398*983/1000 (1361.846)

and
attr(k, "nn.dist")[1,2] would go from 1687.901 to 1687.901*362/1000 (611.02) etc.

I am able to do this one at a time with the following code:

attr(k, "nn.dist")[1,1] = attr(k, "nn.dist")[1,1]*attr(k, "nn.index")[1,1]/1000

But I need a way to do this to every element, without writing it every time…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:17:34+00:00Added an answer on June 10, 2026 at 10:17 am

    The basic operators *, +, / and - are vectorised and will work elementwise on an array.

    Therefore it is as easy as

    attr(k, "nn.dist") * attr(k, "nn.index") /1000
    

    If you only want to do this to the first row of nn.dist, then

    attr(k, "nn.dist")[1,]* attr(k, "nn.index")[1,] /1000
    

    You could reassign to the nn.dist attribute of k, but I would suggest copying to ensure that you know that it is not the original output eg

    k_adj <- k
    attr(k_adj, "nn.dist") <- attr(k_adj, "nn.dist") * attr(k_adj, "nn.index") /1000
    

    or you could simply add a new attribute to the original

     attr(k, 'nn.dist.adj') <- attr(k, "nn.dist") * attr(k, "nn.index") /1000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that uses Sequence objects to read data from a
Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code, which uses a stream to open and modify an
I have the following code to scroll images in vertical direction (uses jCarouselLite )
I have the following code, in which Boost.Local uses a function callback to load
I have following code for inserting data into database using PDO. It inserts data
I have the following code, which uses a Unicode string class from a library
I have the following code which uses an array to write the result to
I have the following code which uses the System.Timers.Timer: // an instance variable Timer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.