[R] why outer function is failing?

David Carlson dc@r|@on @end|ng |rom t@mu@edu
Mon May 4 21:18:40 CEST 2020


The FUN= argument must be a vectorized function (see the documentation,
?outer), but the function rolldie takes only scalar values as arguments:

rolldie(x, y)
Error in rep("X", times) : invalid 'times' argument
In addition: Warning messages:
1: In 1:times : numerical expression has 6 elements: only the first used
2: In 1:nsides : numerical expression has 8 elements: only the first used

David Carlson
Anthropology Department
Texas A&M University

On Mon, May 4, 2020 at 1:54 PM Yousri Fanous <yousri.fanous using gmail.com>
wrote:

> Hello
>
> From outer help page:
> outer takes two vectors
> <
> https://urldefense.proofpoint.com/v2/url?u=https-3A__renenyffenegger.ch_notes_development_languages_R_data-2Dstructures_vector_index&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=IdpldO8NxT8R9A1xPy5xD8VynSPRwZchLY3bUEufCYE&s=aLWLgMoHHIk7DaXgX4L0emzN-KDe2WFMYPpVwZuk35U&e=
> >
> and a function (that itself takes two arguments) and builds a matrix
> <
> https://urldefense.proofpoint.com/v2/url?u=https-3A__renenyffenegger.ch_notes_development_languages_R_data-2Dstructures_matrix_index&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=IdpldO8NxT8R9A1xPy5xD8VynSPRwZchLY3bUEufCYE&s=j5k3zAaxPR8LMHbTe72GTbSGOV_RE5K1Uc2jytaB8SE&e=
> >
> by calling the given function for each combination of the elements in the
> two vectors.
>
> x<-1:6
> y<-3:10
>
>  m<-outer (x,y,function (x,y) rnorm(x,y))
> works as expected.
>
> But now when I replace rnorm with rolldie from package (prob) outer
> complains
>  library (prob)
> m<-outer (x,y,function (x,y) nrow(rolldie(x,y)))
> Error in rep("X", times) : invalid 'times' argument
> In addition: Warning messages:
> 1: In 1:times : numerical expression has 48 elements: only the first used
> 2: In 1:nsides : numerical expression has 48 elements: only the first used
>
> nrow(rolldie(5,4))
> [1] 1024
>
> 1) why outer is failing with rolldie?
> 2) What does the error mean?
>
> As a workaround I can do this thru a double loop, but I was hoping to get a
> more efficient way.
>
> Thanks for the help
>
> Yousri Fanous
> Software developer
> IBM Canada
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=IdpldO8NxT8R9A1xPy5xD8VynSPRwZchLY3bUEufCYE&s=6jxhmxrSNqyZS4-oU2g8r2R0LEZ0yhtSm-4GdfP0Cbk&e=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=IdpldO8NxT8R9A1xPy5xD8VynSPRwZchLY3bUEufCYE&s=rFq4TmerLd5tegHnWSbx5ISdfDNks-TIa9Whne6bBaM&e=
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list