Hello>From outer help page:outer takes two vectors <https://renenyffenegger.ch/notes/development/languages/R/data-structures/vector/index> and a function (that itself takes two arguments) and builds a matrix <https://renenyffenegger.ch/notes/development/languages/R/data-structures/matrix/index> 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]]
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 at 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 at 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]]
The outer function only calls FUN once with two vectors representing all combinations of the inputs. If rolldie is not vectorized then it will have trouble with this input. Why aren't you using sample? On May 4, 2020 11:51:03 AM PDT, Yousri Fanous <yousri.fanous at gmail.com> wrote:>Hello > >From outer help page: >outer takes two vectors ><https://renenyffenegger.ch/notes/development/languages/R/data-structures/vector/index> >and a function (that itself takes two arguments) and builds a matrix ><https://renenyffenegger.ch/notes/development/languages/R/data-structures/matrix/index> >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 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.-- Sent from my phone. Please excuse my brevity.
Re-reading your question it appears you want the number of permutations for each pair, e.g. the number of permutations of the numbers 1 - 3 taken 1 time, etc. If I am correct, this should get you want you want: ct <- outer(y, x, "^") ct [,1] [,2] [,3] [,4] [,5] [,6] [1,] 3 9 27 81 243 729 [2,] 4 16 64 256 1024 4096 [3,] 5 25 125 625 3125 15625 [4,] 6 36 216 1296 7776 46656 [5,] 7 49 343 2401 16807 117649 [6,] 8 64 512 4096 32768 262144 [7,] 9 81 729 6561 59049 531441 [8,] 10 100 1000 10000 100000 1000000 David L Carlson Anthropology Department Texas A&M University On Mon, May 4, 2020 at 2:18 PM David Carlson <dcarlson at tamu.edu> wrote:> 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 at 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 at 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]]