[R] lapply and runif issue?
Bert Gunter
bgunter.4567 at gmail.com
Wed Nov 15 04:06:46 CET 2017
Thanks, Ista. That explains it.
What I missed is the following "note" in ?lapply:
"This means that the recorded call is always of the form FUN(X[[i]], ...),
with i replaced by the current (integer or double) index. "
That being the case, X[[i]] gets passed to the first available argument,
which for runif(n=3, min, max) is the min argument, as you said. This is a
subtlety (to me, anyway) of which I was unaware. Which is why I hesitated
to call it a bug. It ain't! It is documented -- I just failed to read
carefully enough.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Nov 14, 2017 at 6:11 PM, Ista Zahn <istazahn at gmail.com> wrote:
> Hi Bert,
>
> On Tue, Nov 14, 2017 at 8: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
> >
>
> Exactly what expectation do you imagine the above is consistent with? Does
>
> > lapply(100*(1:3), rnorm, n = 3)
> [[1]]
> [1] 100.35425 99.29429 98.69429
>
> [[2]]
> [1] 198.2963 201.1031 201.1077
>
> [[3]]
> [1] 299.7012 298.3700 298.0684
>
> change your assessment?
>
> >
> > ## 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
>
> The first argument to runif is named 'n'. Thus,
>
> lapply(1:3, runif)
>
> means roughly
>
> list(runif(n = 1), runif(n = 2), runif(n = 3))
>
> But you specify than lapply(1:3, runif, n = 3). Since the first
> argument ('n') is already specified, the X values from lapply get
> "pushed" to the second argument. That is,
>
> lapply(1:3, runif, n = 3)
>
> means roughly
>
> list(runif(n = 3, min = 1), runif(n = 3, min = 2), runif(n = 3, min = 3))
>
> Note that this is exactly the same thing that happens with
>
> lapply(1:3, rnorm, n = 3), though it becomes more obvious with
>
> lapply(100*(1:3), rnorm, n = 3)
>
> That is,
>
> lapply(1:3, rnorm, n = 3)
>
> means roughly
>
> list(rnorm(n = 3, mean = 1), rnorm(n = 3, mean = 2), rnorm(n = 3, mean =
> 3))
>
> >
> >
> > ## 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
>
> Sure, because you never use x in the body of your anonymous function.
>
> As a final note, what you seem to expect can be achieved with
>
> replicate(3, rnorm(n = 3), simplify = FALSE)
>
> and
>
> replicate(3, runif(n = 3), simplify = FALSE)
>
>
> Best,
> Ista
>
> >
> >
> >
> > 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