Skip to content
All library documents

Using DuckDB in R to Store and Query Stock Price Data

Article Robot Wealth

Summary

This beginner guide demonstrates an R workflow for managing stock price data with DuckDB. It explains why a database can help organize and query growing datasets, while noting tradeoffs such as setup, SQL knowledge, resource use, and reduced readability. DuckDB is presented as an embedded analytical database that can run within an R session, with either an in-memory connection or a database stored on disk.

The example imports CSV price histories for several stocks, writes them to a table, and removes the original data frame. It shows how dplyr operations can query the table without loading all rows into R, and how collect returns results to the session. The author also illustrates executing SQL directly, computing a moving average with a window function, updating a table, and inspecting SQL generated by dplyr. Sample outputs demonstrate the workflow, but do not constitute a benchmark or investment result. The examples use a small stock universe; users working with other datasets should check query translation, data quality, and persistence behavior for their own setup.

Key ideas

  • DuckDB can run embedded in an R workflow and can use either in-memory or persistent storage.
  • Writing price data to a database table makes it possible to query data without collecting the full table into R.
  • Dplyr translates supported operations into SQL, while some calculations may require SQL or a rewritten expression.
  • SQL window functions can calculate rolling measures and update values stored in a table.
  • The example demonstrates data handling techniques rather than trading performance or database benchmarks.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.