Table function causes R to lockup

Hi All,

I am new at R scripting. Justs noticed today that when using the airquality database by running the following command> table(airquality)
R locks up and can't be recovered.

You are probably consuming all your available memory.
What outcome are you hoping to acheive by the table command on an entire data.frame? its typically used to make cross tables of two vectors.

table() counts the frequencies of elements. For a single vector, it will give simple counts

table(airquality$Month)
#> 
#>  5  6  7  8  9 
#> 31 30 31 31 30

For two vectors (or a dataframe with two columns), it will count the frequency of combinations. Here we can see one observation for each Month x Day pair except for months without a 31st day.

table(airquality$Month, airquality$Day)
#>    
#>     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#>   5 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
#>   6 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
#>   7 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
#>   8 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
#>   9 1 1 1 1 1 1 1 1 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
#>    
#>     29 30 31
#>   5  1  1  1
#>   6  1  1  0
#>   7  1  1  1
#>   8  1  1  1
#>   9  1  1  0

Adding a third variable will then count all of the triplets, so now Month x Day x Temp has 6200 elements. This is huge increase in size.

length(
  table(airquality$Month, airquality$Day, airquality$Temp)
)
#> 6200

The problem with table(airquality) is that it does this for all six columns and it exhausts the available memory completely.

table(airquality)
#> Error: cannot allocate vector of size 11.2 Gb

When playing with R functions to learn them, try looking at a reference page like ?table to see the examples and tweak them. These tend to be safe.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.