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 8766835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:38:57+00:00 2026-06-13T16:38:57+00:00

Possible Duplicate: Why are loops slow in R? Consider the following task. A dataset

  • 0

Possible Duplicate:
Why are loops slow in R?

Consider the following task. A dataset has 40 variables for 20,000 “users”. Each user has between 1 and 150 observations. All users are stacked in a matrix called data. The first column is the id of the user and identifies the user. All id are stored in a 20,000 X 1 matrix called userid.

Consider the following R code

useridl = length(userid)
itime=proc.time()[3]    
for (i in 1:useridl) {
temp =data[data[,1]==userid[i],]
   }
 etime=proc.time()[3]
 etime-itime

This code just goes through the 20,000 users, creating the temp matrix every time. With the subset of observations belonging to userid[i]. It takes about 6 minutes in a MacPro.

In MatLab, the same task

tic
for i=1:useridl
temp=data(data(:,1)==userid(i),:);
end
toc

takes 1 minute.

Why is R so much slower? This is standard task, I am using matrices in both cases. Any ideas?

  • 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-13T16:38:58+00:00Added an answer on June 13, 2026 at 4:38 pm

    As @joran commented, that’s bad R practice. Instead of repeatedly subsetting your original matrix, just put the subsets in a list once and then iterate over the list with lapply or similar.

    # make example data
    set.seed(21)
    userid <- 1:1e4
    obs <- sample(150, length(userid), TRUE)
    users <- rep(userid, obs)
    Data <- cbind(users,matrix(rnorm(40*sum(obs)),sum(obs),40))
    
    # reorder so Data isn't sorted by userid
    Data <- Data[order(Data[,2]),]
    # note that you have to call the data.frame method explicitly,
    # the default method returns a vector
    system.time(temp <- split.data.frame(Data, Data[,1])) ## Returns times in seconds
    #    user  system elapsed 
    #    2.84    0.08    2.92 
    

    My guess is that the garbage collector is slowing down your R code, since you’re continually overwriting the temp object.

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

Sidebar

Related Questions

Possible Duplicate: Why avoid while loops? I am trying to learn Python by following
Possible Duplicate: JavaScript for…in vs for difference between for..in and for loops, and counter
Possible Duplicate: Difference between declaring variables before or in loop? Is there any (or
Possible Duplicate: Difference between declaring variables before or in loop? I always wondered, is
Possible Duplicate: If statement in my loops are being skipped in C Having a
Possible Duplicate: Loop code for each file in a directory I assumed that I
Possible Duplicate: Display numbers from 1 to 100 without loops or conditions Interview question:
Possible Duplicate: Breaking out of a nested loop How to exit from nested loops
Possible Duplicate: Joining List has integer values with python I'm having a syntax problem
Possible Duplicate: C header file loops Original Question: I always had problems understanding why

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.