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

  • Home
  • SEARCH
  • 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 7597645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:12:25+00:00 2026-05-30T22:12:25+00:00

I have a double loop that I not only don’t like, but would take

  • 0

I have a double loop that I not only don’t like, but would take 14 days to run on my computer since it is going over 3200 records and 1090 variables at about .12 per iteration.

A smaller reproducible bit. It simply checks how many numbers are in the same column between two records, not including NA’s. Then it attaches the results to the original data frame.

y <- data.frame(c(1,2,1,NA,NA),c(3,3,3,4,NA),c(5,4,5,7,7),c(7,8,7,9,10))
resultdf <- NULL
for(i in 1:nrow(y))
{
  results <- NULL
  for(j in 1:nrow(y))
  {
    results <- c(results,sum((y[i,]==y[j,]),na.rm=TRUE))
  }
  resultdf <- cbind(resultdf,results)
}
y <- cbind(y,resultdf)

I have repeat calculations that could possibly be avoided leaving about 7 days.

If I understand correctly, a few apply functions are in C that might be faster. I haven’t been able to get any to work though. I’m also curious if there is a package that would run faster. Can anyone help speed up the calculation?

Thank you!

  • 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-05-30T22:12:27+00:00Added an answer on May 30, 2026 at 10:12 pm

    I have created data to your specifications, and using @BenBolker’s suggestion about using a matrix:

    > y <- matrix(sample(c(1:9, NA), 3200 * 1090, replace = TRUE),
    +             nrow = 3200, ncol = 1090)
    

    and compared the computation times for three different implementations:

    f1 was suggested by @Andrei:

    > f1 <- function(y)apply(y, 1, function(r1)
    +                  apply(y, 1, function(r2)sum(r1==r2, na.rm=TRUE)))
    
    > system.time(r1 <- f1(y))
       user  system elapsed 
     523.51    0.77  528.73 
    

    f2 was suggested by @VincentZoonekynd:

    > f2 <- function(y) {
    +   f <- function(i,j) sum(y[i,] == y[j,], na.rm=TRUE)
    +   d <- outer( 1:nrow(y), 1:nrow(y), Vectorize(f) )
    +   return(d)
    + }
    > system.time(r2 <- f2(y))
       user  system elapsed 
     658.94    1.96  710.67
    

    f3 is a double loop over the upper triangle as suggested by @BenBolker. It is also a bit more efficient than your OP in that it pre-allocates the output matrix:

    > f3 <- function(y) {
    +   result <- matrix(NA, nrow(y), nrow(y))
    +   for (i in 1:nrow(y)) {
    +     row1 <- y[i, ]
    +     for (j in i:nrow(y)) {
    +       row2 <- y[j, ]
    +       num.matches  <- sum(row1 == row2, na.rm = TRUE)
    +       result[i, j] <- num.matches
    +       result[j, i] <- num.matches
    +     }
    +   }
    +   return(result)
    + }
    
    > system.time(r3 <- f3(y))
       user  system elapsed 
     167.66    0.08  168.72 
    

    So the double loop is the fastest of all three, although not as elegant and compact as the other two answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that runs fairly well, but I would like to make
I have a double value f and would like a way to nudge it
I have a double thats got a value of something like 0.50000 but I
If I have a double (234.004223), etc., I would like to round this to
I have double (or float) variables that might be empty, as in holding no
I have a specific requirement that all children of a particular JComponent have double
I'm experimenting with some multithreading constructions, but somehow it seems that multithreading is not
If I have a for loop that reads from a data file and each
I have a view that I created in my GWT application and I would
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give

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.