[R] lapply and runif issue?

William Dunlap wdunlap at tibco.com
Wed Nov 15 17:38:32 CET 2017


Your lapply is making the call
   runif(n=3, min=i)
for i in 1:3.  That runif's 3 argument is 'max', with default value 1
so that is equivalent to calling
   runif(n=3, min=i, max=1)
When i>max, outside the domain of the family of uniform distributions,
runif returns NaN's.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Nov 14, 2017 at 5:11 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:

> Could someone please explain the following? I did check bug reports, but
> did not recognize the issue there. I am reluctant to call it a bug, as it
> is much more likely my misunderstanding. Ergo my request for clarification:
>
> ## As expected:
>
> > lapply(1:3, rnorm, n = 3)
> [[1]]
> [1] 2.481575 1.998182 1.312786
>
> [[2]]
> [1] 2.858383 1.827863 1.699015
>
> [[3]]
> [1] 1.821910 2.530091 3.995677
>
>
> ## Unexpected by me:
>
> > lapply(1:3, runif, n = 3)
> [[1]]
> [1] 1 1 1
>
> [[2]]
> [1] NaN NaN NaN
>
> [[3]]
> [1] NaN NaN NaN
>
> Warning messages:
> 1: In FUN(X[[i]], ...) : NAs produced
> 2: In FUN(X[[i]], ...) : NAs produced
>
>
> ## But note, as expected:
>
> > lapply(1:3, function(x)runif(3))
> [[1]]
> [1] 0.2950459 0.8490556 0.4303680
>
> [[2]]
> [1] 0.5961144 0.5330914 0.2363679
>
> [[3]]
> [1] 0.8079495 0.1431838 0.3671915
>
>
>
> Many thanks for any clarification.
>
> -- Bert
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list