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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:55:56+00:00 2026-06-07T10:55:56+00:00

Suppose I have a large matrix: M <- matrix(rnorm(1e7),nrow=20) Further suppose that each column

  • 0

Suppose I have a large matrix:

M <- matrix(rnorm(1e7),nrow=20)

Further suppose that each column represents a sample. Say I would like to apply t.test() to each column, is there a way to do this that is much faster than using apply()?

apply(M, 2, t.test)

It took slightly less than 2 minutes to run the analysis on my computer:

> system.time(invisible( apply(M, 2, t.test)))
user  system elapsed 
113.513   0.663 113.519 
  • 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-07T10:55:58+00:00Added an answer on June 7, 2026 at 10:55 am

    If you have a multicore machine there are some gains from using all the cores, for example using mclapply.

    > library(multicore)
    > M <- matrix(rnorm(40),nrow=20)
    > x1 <- apply(M, 2, t.test)
    > x2 <- mclapply(1:dim(M)[2], function(i) t.test(M[,i]))
    > all.equal(x1, x2)
    [1] "Component 1: Component 9: 1 string mismatch" "Component 2: Component 9: 1 string mismatch"
    # str(x1) and str(x2) show that the difference is immaterial
    

    This mini-example shows that things go as we planned. Now scale up:

    > M <- matrix(rnorm(1e7), nrow=20)
    > system.time(invisible(apply(M, 2, t.test)))
       user  system elapsed 
    101.346   0.626 101.859
    > system.time(invisible(mclapply(1:dim(M)[2], function(i) t.test(M[,i]))))
      user  system elapsed 
    55.049   2.527  43.668
    

    This is using 8 virtual cores. Your mileage may vary. Not a huge gain, but it comes from very little effort.

    EDIT

    If you only care about the t-statistic itself, extracting the corresponding field ($statistic) makes things a bit faster, in particular in the multicore case:

    > system.time(invisible(apply(M, 2, function(c) t.test(c)$statistic)))
       user  system elapsed 
     80.920   0.437  82.109 
    > system.time(invisible(mclapply(1:dim(M)[2], function(i) t.test(M[,i])$statistic)))
       user  system elapsed 
     21.246   1.367  24.107
    

    Or even faster, compute the t value directly

    my.t.test <- function(c){
      n <- sqrt(length(c))
      mean(c)*n/sd(c)
    }
    

    Then

    > system.time(invisible(apply(M, 2, function(c) my.t.test(c))))
       user  system elapsed 
     21.371   0.247  21.532 
    > system.time(invisible(mclapply(1:dim(M)[2], function(i) my.t.test(M[,i]))))
       user  system elapsed 
    144.161   8.658   6.313 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a large collection of integers (say 50,000,000 of them). I would
I have a very large list Suppose I do that (yeah, I know the
Suppose I have a rather large class Matrix , and I've overloaded operator== to
Suppose you have a really large table, say a few billion unordered rows, and
Suppose that you have a large collection of key/value pairs, where the value is
Suppose I have an ifstream which represents a large file containing lots of sub-files
Suppose I have a large .swf file. Say, it's 10MB. I don't want the
I have a large array of comments that are threaded. Suppose the comments look
Suppose I have a global variable that contains a large structure: typedef struct {
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')

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.