{"id":101,"date":"2012-10-23T11:57:25","date_gmt":"2012-10-23T11:57:25","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=101"},"modified":"2023-08-20T11:07:19","modified_gmt":"2023-08-20T15:07:19","slug":"handy-r-function-expand-grid","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/handy-r-function-expand-grid\/","title":{"rendered":"Handy R Function: expand.grid"},"content":{"rendered":"<p>In graduate school I took an experimental design class. As expected, we were assigned a group project that required us to design and analyze our own experiment. Two other students and I got together and decided to design an experiment involving paper airplanes. Not terribly useful, but it&#8217;s not as if we had a grant from the NSF. Anyway, we wanted to see what caused a plane to fly farther. Was it design? Was it paper weight? Was it both? We ended up running a 3 x 3 factorial experiment with the paper airplane throwers (us) as blocks.<\/p>\n<p>We had three different paper airplane designs, three different paper weights, three people throwing, and replications. If memory serves, replications was three. In other words, each of us threw each of the 9 airplanes ( 3 designs x 3 paper weights) three times. That came out to 81 throws.<\/p>\n<p>Now we had to randomly pick an airplane and throw it. How to randomize? Here&#8217;s a handy way to do that in R. First we use the <strong>expand.grid<\/strong> function. This creates a data frame from all combinations of \u00c2\u00a0factors:<\/p>\n<pre>d <- expand.grid(throw=c(1,2,3), \r\n                      design=c(1,2,3), \r\n                      paper=c(20,22,28), \r\n                      person=c(\"Jack\",\"Jane\",\"Jill\"))<\/pre>\n<pre>head(d)\r\n throw design paper person\r\n1 1 1 20 Jack\r\n2 2 1 20 Jack\r\n3 3 1 20 Jack\r\n4 1 2 20 Jack\r\n5 2 2 20 Jack\r\n6 3 2 20 Jack<\/pre>\n<p>We see Jack will throw design 1 made of 20 lb. paper three times. Then he'll throw design 2 made of 20 lb. paper three times. And so on. Now to randomly sort the data frame:<\/p>\n<pre>d_rand <- d[sample(1:nrow(d)),]<\/pre>\n<pre>d_rand\r\n throw design paper person\r\n34 1 3 20 Jane\r\n20 2 1 28 Jack\r\n3 3 1 20 Jack\r\n53 2 3 28 Jane\r\n37 1 1 22 Jane\r\n77 2 2 28 Jill<\/pre>\n<p>Very handy! And nifty, too.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In graduate school I took an experimental design class. As expected, we were assigned a group project that required us&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/handy-r-function-expand-grid\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":3,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/101\/revisions\/888"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}