user28575 user28575 - 4 years ago 100
R Question

What does map(-2:2, rnorm, n = 5) do? Why? What does map_dbl(-2:2, rnorm, n = 5) do? Why?

This is a problem in R for data science by Hadley.
The results for map(-2:2, rnorm, n = 5) is a list of 5 double vector.
and map_dbl(-2:2, rnorm, n = 5) is a error says "Error: Result 1 is not a length 1 atomic vector".
But I don't know why.

Answer Source

map(-2:2, rnorm, n = 5) is the list of vectors rnorm(-2, n=5), rnorm(-1, n=5), rnorm(0, n=5), rnorm(1, n=5), rnorm(2, n=5).

Since these vectors have not length 1, map_dbl(-2:2, rnorm, n = 5) throws an error. For example map_dbl(-2:2, rnorm, n = 1) does not return an error, it is the vector made of rnorm(-2, n=1), rnorm(-1, n=1), rnorm(0, n=1), rnorm(1, n=1), rnorm(2, n=1).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download