Skip to content
Home ยป C Stack Usage is close to the limit and how to solve it

C Stack Usage is close to the limit and how to solve it

Error: C stack usage <numbers> is too close to the limit

If you are running into this error in R, chances are that you are running a recursive function that runs too deep. I found this page to be particularly good in explaining the concept. But here’s an example:

function pop_pringles:
   if tube == empty:

In this example, we run the pop_pringles function and we will keep eating it until the tube is empty. It’s a function that calls upon itself until a condition has been met.

Running into the C stack error oftentimes means that you’re letting a recursive function run indefinitely. Here’s some examples I ran into myself and which I found on the web.

Example one: merging a huge data table with another data table that didn’t exist.

dt <- merge(dt, another_dt, by = 'ID', all.x = TRUE)

Example two: Creating a super long string. Instead of the col argument, the sep argument should have been used. (Source: Stackoverflow 14719349)

output_table_subset = mutate(big_data_frame,
     combined_table = paste0(first_part, second_part, col = "_"))

Example three: Two functions calling each other indefinitely. (Source: Stackoverflow 14719349)

change_to_factor <- function(x){
  x <- change_to_character(x)

change_to_character <- function(x){
  x <- change_to_factor(x)


In other words, check if something is running indefinitely. If that is not the case it might be a package error. For example, the Google Sheets package is known to have thrown this error in certain package versions.

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Good luck!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

2 thoughts on “C Stack Usage is close to the limit and how to solve it”

  1. Thank you for going above and beyond in providing such valuable information on this website. Your dedication to sharing knowledge is making a positive impact, and I’m truly thankful for your contributions. The articles are well-researched, engaging, and cover a wide range of topics. Thank you for curating such an exceptional platform. Thanks for the informations! ID : CMT-94LSJIZNB8OYUL59L2

Leave a Reply

Your email address will not be published. Required fields are marked *