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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:43:28+00:00 2026-06-16T09:43:28+00:00

I have a quite large data frame, about 10 millions of rows. It has

  • 0

I have a quite large data frame, about 10 millions of rows. It has columns x and y, and what I want is to compute

hypot <- function(x) {sqrt(x[1]^2 + x[2]^2)}

for each row. Using apply it would take a lot of time (about 5 minutes, interpolating from lower sizes) and memory.

But it seems to be too much for me, so I’ve tried different things:

  • compiling the hypot function reduces the time by about 10%
  • using functions from plyr greatly increases the running time.

What’s the fastest way to do this thing?

  • 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-16T09:43:29+00:00Added an answer on June 16, 2026 at 9:43 am

    What about with(my_data,sqrt(x^2+y^2)) ?

    set.seed(101)
    d <- data.frame(x=runif(1e5),y=runif(1e5))
    
    library(rbenchmark)
    

    Two different per-line functions, one taking advantage of vectorization:

    hypot <- function(x) sqrt(x[1]^2+x[2]^2)
    hypot2 <- function(x) sqrt(sum(x^2))
    

    Try compiling these too:

    library(compiler)
    chypot <- cmpfun(hypot)
    chypot2 <- cmpfun(hypot2)
    
    benchmark(sqrt(d[,1]^2+d[,2]^2),
              with(d,sqrt(x^2+y^2)),
              apply(d,1,hypot),
              apply(d,1,hypot2),
              apply(d,1,chypot),
              apply(d,1,chypot2),
              replications=50)
    

    Results:

                           test replications elapsed relative user.self sys.self
    5       apply(d, 1, chypot)           50  61.147  244.588    60.480    0.172
    6      apply(d, 1, chypot2)           50  33.971  135.884    33.658    0.172
    3        apply(d, 1, hypot)           50  63.920  255.680    63.308    0.364
    4       apply(d, 1, hypot2)           50  36.657  146.628    36.218    0.260
    1 sqrt(d[, 1]^2 + d[, 2]^2)           50   0.265    1.060     0.124    0.144
    2  with(d, sqrt(x^2 + y^2))           50   0.250    1.000     0.100    0.144
    

    As expected the with() solution and the column-indexing solution à la Tyler Rinker are essentially identical; hypot2 is twice as fast as the original hypot (but still about 150 times slower than the vectorized solutions). As already pointed out by the OP, compilation doesn’t help very much.

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

Sidebar

Related Questions

I have quite a relatively large number of data, it has 80 columns and
I have an Scrollview in my app that has quite a large amount of
To avoid maintaining complex data structures, I want to allocate blocks with quite large
I have quite a large VB6 project, with many forms, classes, modules and user
I have quite a large table with 19 000 000 records, and I have
I have quite a large XSL document for an assignment that does a number
I have two quite large tables, let's call them 'authors' and 'articles'. Every row
I have a several functions which are quite large, and are only used in
I have a multi-line text view that can get quite large. When the user
I have a web application project that works with a quite large database (over

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.