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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:46:22+00:00 2026-06-12T04:46:22+00:00

I have a very large training set (~2Gb) in a CSV file. The file

  • 0

I have a very large training set (~2Gb) in a CSV file. The file is too large to read directly into memory (read.csv() brings the computer to a halt) and I would like to reduce the size of the data file using PCA. The problem is that (as far as I can tell) I need to read the file into memory in order to run a PCA algorithm (e.g., princomp()).

I have tried the bigmemory package to read the file in as a big.matrix, but princomp doesn’t function on big.matrix objects and it doesn’t seem like big.matrix can be converted into something like a data.frame.

Is there a way of running princomp on a large data file that I’m missing?

I’m a relative novice at R, so some of this may be obvious to more seasoned users (apologies in avance).

Thanks for any info.

  • 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-12T04:46:23+00:00Added an answer on June 12, 2026 at 4:46 am

    The way I solved it was by calculating the sample covariance matrix iteratively. In this way you only need a subset of the data for any point in time. Reading in just a subset of the data can be done using readLines where you open a connection to the file and read iteratively. The algorithm looks something like (it is a two-step algorithm):

    Calculate the mean values per column (assuming that are the variables)

    1. Open file connection (con = open(...))
    2. Read 1000 lines (readLines(con, n = 1000))
    3. Calculate sums of squares per column
    4. Add those sums of squares to a variable (sos_column = sos_column + new_sos)
    5. Repeat 2-4 until end of file.
    6. Divide by number of rows minus 1 to get the mean.

    Calculate the covariance matrix:

    1. Open file connection (con = open(...))
    2. Read 1000 lines (readLines(con, n = 1000))
    3. Calculate all cross-products using crossprod
    4. Save those crossproducts in a variable
    5. Repeat 2-4 until end of file.
    6. divide by the number of rows minus 1 to get the covariance.

    When you have the covariance matrix, just call princomp with covmat = your_covmat and princomp will skip calulating the covariance matrix himself.

    In this way the datasets you can process are much, much larger than your available RAM. During the iterations, the memory usage is roughly the memory the chunk takes (e.g. 1000 rows), after that the memory usage is limited to the covariance matrix (nvar * nvar doubles).

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

Sidebar

Related Questions

I have a very large csv file (about 91 million rows so a for
I have a very large gzip csv file (around 500GB) which I need to
I have a very large Excel sheet converted from a 6000 page PDF file,
I have a very large data file with around 60000 rows. I need to
I have a very large possible data set that I am trying to visualize
I have a very large XML file which has like 40000 data, and when
I have a very large set of permissions in my application that I represent
Suppose, I have very large foreach loop. I want put this loop into try
I have very large HTML that, if being parsed into DOM tree, would take
I have a very large file as: filename.bz2 , how can I view the

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.