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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:18:21+00:00 2026-05-16T01:18:21+00:00

Suppose that you have a data frame with many rows and many columns. The

  • 0

Suppose that you have a data frame with many rows and many columns.

The columns have names. You want to access rows by number, and columns by name.

For example, one (possibly slow) way to loop over the rows is

for (i in 1:nrow(df)) {
  print(df[i, "column1"])
  # do more things with the data frame...
}

Another way is to create “lists” for separate columns (like column1_list = df[["column1"]), and access the lists in one loop. This approach might be fast, but also inconvenient if you want to access many columns.

Is there a fast way of looping over the rows of a data frame? Is some other data structure better for looping fast?

  • 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-16T01:18:22+00:00Added an answer on May 16, 2026 at 1:18 am

    I think I need to make this a full answer because I find comments harder to track and I already lost one comment on this… There is an example by nullglob that demonstrates the differences among for, and apply family functions much better than other examples. When one makes the function such that it is very slow then that’s where all the speed is consumed and you won’t find differences among the variations on looping. But when you make the function trivial then you can see how much the looping influences things.

    I’d also like to add that some members of the apply family unexplored in other examples have interesting performance properties. First I’ll show replications of nullglob’s relative results on my machine.

    n <- 1e6
    system.time(for(i in 1:n) sinI[i] <- sin(i))
      user  system elapsed 
     5.721   0.028   5.712 
    
    lapply runs much faster for the same result
    system.time(sinI <- lapply(1:n,sin))
       user  system elapsed 
      1.353   0.012   1.361 
    

    He also found sapply much slower. Here are some others that weren’t tested.

    Plain old apply to a matrix version of the data…

    mat <- matrix(1:n,ncol =1),1,sin)
    system.time(sinI <- apply(mat,1,sin))
       user  system elapsed 
      8.478   0.116   8.531 
    

    So, the apply() command itself is substantially slower than the for loop. (for loop is not slowed down appreciably if I use sin(mat[i,1]).

    Another one that doesn’t seem to be tested in other posts is tapply.

    system.time(sinI <- tapply(1:n, 1:n, sin))
       user  system elapsed 
     12.908   0.266  13.589 
    

    Of course, one would never use tapply this way and it’s utility is far beyond any such speed problem in most cases.

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

Sidebar

Related Questions

Suppose I have an R data frame with columns that specify location (lat/long), height,
Suppose that I have an array defined by: data = np.array([('a1v1', 'a2v1', 'a3v1', 'a4v1',
I am trying to create a UITableView that only displays the number of rows
I'm using fixtures to hold all the test data that I have. Each row
So I have kind of a ignorant (maybe?) question. I'm working with writing to
I got a project in mind that makes it worth to finally take the
I've been banging my head for a long time on this one I am
Maybe it has been asked somewhere, but I am trying to find my question
I'm still trying to learn SQL as of this writing, so I'm a bit
I'm developing a shopping comparison website, and the project is in a very advanced

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.