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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:17:42+00:00 2026-06-03T02:17:42+00:00

Suppose I have the following frequency table. > print(dat) V1 V2 1 1 11613

  • 0

Suppose I have the following frequency table.

> print(dat)
V1    V2
1  1 11613
2  2  6517
3  3  2442
4  4   687
5  5   159
6  6    29

# V1 = Score
# V2 = Frequency

How can I efficiently compute the Mean and standard deviation?
Yielding: SD=0.87 MEAN=1.66.
Replicating the score by frequency takes too long to compute.

  • 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-03T02:17:44+00:00Added an answer on June 3, 2026 at 2:17 am

    Mean is easy. SD is a little trickier (can’t just use fastmean() again because there’s an n-1 in the denominator.

    > dat <- data.frame(freq=seq(6),value=runif(6)*100)
    > fastmean <- function(dat) {
    +   with(dat, sum(freq*value)/sum(freq) )
    + }
    > fastmean(dat)
    [1] 55.78302
    > 
    > fastRMSE <- function(dat) {
    +   mu <- fastmean(dat)
    +   with(dat, sqrt(sum(freq*(value-mu)^2)/(sum(freq)-1) ) )
    + }
    > fastRMSE(dat)
    [1] 34.9316
    > 
    > # To test
    > expanded <- with(dat, rep(value,freq) )
    > mean(expanded)
    [1] 55.78302
    > sd(expanded)
    [1] 34.9316
    

    Note that fastRMSE calculates sum(freq) twice. Eliminating this would probably result in another minor speed boost.

    Benchmarking

    > microbenchmark(
    +   fastmean(dat),
    +   mean( with(dat, rep(value,freq) ) )
    +   )
    Unit: microseconds
                                   expr    min      lq median     uq    max
    1                     fastmean(dat) 12.433 13.5335 14.776 15.398 23.921
    2 mean(with(dat, rep(value, freq))) 21.225 22.3990 22.714 23.406 86.434
    > dat <- data.frame(freq=seq(60),value=runif(60)*100)
    > 
    > dat <- data.frame(freq=seq(60),value=runif(60)*100)
    > microbenchmark(
    +   fastmean(dat),
    +   mean( with(dat, rep(value,freq) ) )
    +   )
    Unit: microseconds
                                   expr    min     lq  median      uq     max
    1                     fastmean(dat) 13.177 14.544 15.8860 17.2905  54.983
    2 mean(with(dat, rep(value, freq))) 42.610 48.659 49.8615 50.6385 151.053
    > dat <- data.frame(freq=seq(600),value=runif(600)*100)
    > microbenchmark(
    +   fastmean(dat),
    +   mean( with(dat, rep(value,freq) ) )
    +   )
    Unit: microseconds
                                   expr      min       lq    median       uq       max
    1                     fastmean(dat)   15.706   17.489   25.8825   29.615    79.113
    2 mean(with(dat, rep(value, freq))) 1827.146 2283.551 2534.7210 2884.933 26196.923
    

    The replicating solution appears to be O( N^2 ) in the number of entries.

    Replicating solution

    The fastmean solution appears to have a 12ms or so fixed cost after which it scales beautifully.

    More benchmarking

    Comparison with dot product.
    
    dat <- data.frame(freq=seq(600),value=runif(600)*100)
    dbaupp <- function(dat) {
      total.count <- sum(dat$freq)
      as.vector(dat$freq %*% dat$value) / total.count
    }
    microbenchmark(
      fastmean(dat),
      mean( with(dat, rep(value,freq) ) ),
      dbaupp(dat)
    )
    
    Unit: microseconds
                                   expr     min       lq   median       uq       max
    1                       dbaupp(dat)  20.162  21.6875  25.6010  31.3475   104.054
    2                     fastmean(dat)  14.680  16.7885  20.7490  25.1765    94.423
    3 mean(with(dat, rep(value, freq))) 489.434 503.6310 514.3525 583.2790 30130.302
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following table: Table: Score field: ID: uniqueidentifier field: Departmentid: int
Suppose I have the following table GAME match_id, user_id, score 1, 10, 45 1,
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have the following table: PARAMETER - NAME varchar2(10) - TABLE_NAME varchar2(50) -
Suppose I have following table in my database: Tbl_Persons: Id Country Name 1 Australia
Suppose I have the following 2 web service code, I think I can do
Suppose I have the following HTML code, how can I pass the user's input
suppose I have the following source table (called S): name gender code Bob 0
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
suppose I have following sql table objid firstname lastname active 1 test test 0

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.