Home > Enterprise >  What is the difference between numeric() and as.numeric()?
What is the difference between numeric() and as.numeric()?

Time:11-03

The code:

Storage<-numeric(5)
for(i in 1:5){
Storage[i]<-i^2
}

To store the values I create an empty vector which is "Storage". But I do not understand what "numeric(5)" means and especially what 5 means inside the numeric().

Why is just not numeric()? In Python I would do storage = [] and would this be equal to numeric()?

CodePudding user response:

We can easily test why it's smart to pre-allocate a vector of the correct size by doing a simple comparison and benchmarking. To do so, we define a simple function which fills up a length 10,000 numeric vector. We can choose either to pre-allocate the vector using numeric(10000) or not pre-allocate it by using c()

test <- function(preallocate) {
  Storage <- if(preallocate) numeric(10000) else c()
  for(i in 1:10000) {
    Storage[i] <- i
  }
}

Now let's compare how fast this function is when we toggle pre-allocation on and off:

library(microbenchmark)

microbenchmark(test(preallocate = TRUE), test(preallocate = FALSE))
#> Unit: microseconds
#>                       expr    min     lq     mean  median      uq    max neval
#>   test(preallocate = TRUE)  300.3  301.8  331.053  302.50  305.65 2762.4   100
#>  test(preallocate = FALSE) 1474.0 1498.3 1710.614 1519.15 1579.30 3998.8   100

We can see that it is over five times slower to write to a vector that is not pre-allocated.

Incidentally, to answer the question in your post's title, as.numeric and numeric do two different things. numeric creates a vector of zeros of the given length, whereas as.numeric attempts to convert whatever is passed to it as a numeric vector:

as.numeric(5)
#> [1] 5

numeric(5)
#> [1] 0 0 0 0 0

as.numeric("5")
#> [1] 5

numeric("5")
#> [1] 0 0 0 0 0

Created on 2022-11-03 with reprex v2.0.2

  • Related