{"id":26124,"date":"2022-12-15T20:53:18","date_gmt":"2022-12-15T15:23:18","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/"},"modified":"2022-12-15T20:53:18","modified_gmt":"2022-12-15T15:23:18","slug":"solved-could-you-please-help-me-to-understand-an-r-code","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/","title":{"rendered":"[Solved] Could you please help me to understand an R code?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28707145\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28707145\" data-parentid=\"28706755\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>First of all, in general,<\/p>\n<pre><code>var &lt;- expr\n<\/code><\/pre>\n<p>evaluates the R expression <em>expr<\/em> and assigns the result to the variable <em>var<\/em>. If the statement occurs inside a function, then <em>var<\/em> becomes a function-local variable, otherwise, it becomes a global variable.<\/p>\n<pre><code>c(0,0,0,1,0,1,0,1,1,1,1,0)\n<\/code><\/pre>\n<p>Combines 12 <code>double<\/code> literals into a <code>double<\/code> vector in the order given.<\/p>\n<pre><code>matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, byrow=T )\n<\/code><\/pre>\n<p>Creates a <code>matrix<\/code> from the vector with 4 rows and 3 columns, populating the matrix one row at a time from top to bottom (left-to-right within each row).<\/p>\n<pre><code>nrow(patterns)\n<\/code><\/pre>\n<p>Returns the number of rows in the <code>patterns<\/code> matrix.<\/p>\n<pre><code>sample(nrow(patterns))\n<\/code><\/pre>\n<p>Returns an <code>integer<\/code> vector of <code>nrow(patterns)<\/code> elements, by basically randomly scrambling the set of integers from 1 to <code>nrow(patterns)<\/code>.<\/p>\n<pre><code>patterns[sample(nrow(patterns)),]\n<\/code><\/pre>\n<p>Indexes the <code>patterns<\/code> matrix. This type of indexing basically allows you to extract a &#8220;submatrix&#8221; of the original matrix. The argument to the left of the comma specifies the rows to select, and to the right specifies the columns to select. An omitted argument is equivalent to specifying all indexes of that dimension. This particular expression selects all rows and all columns in the matrix, but scrambles the row order.<\/p>\n<pre><code>t(ps)\n<\/code><\/pre>\n<p>Transposes the matrix <code>ps<\/code>.<\/p>\n<pre><code>as.vector(t(ps))\n<\/code><\/pre>\n<p>Flattens the transposed matrix into a vector. Note that this is by column, which is the opposite of how the matrix was originally constructed from a vector earlier. Note that, because this is the last statement in the enclosing function, it will automatically become the return value of the function when it is executed.<\/p>\n<pre><code>function(i) {\n    ps &lt;- patterns[sample(nrow(patterns)),]\n    as.vector(t(ps))\n}\n<\/code><\/pre>\n<p>Defines a function taking one parameter <code>i<\/code> and that executes the two statements I explained above. Note that <code>patterns<\/code> is effectively closured by this function.<\/p>\n<pre><code>1:100\n<\/code><\/pre>\n<p>Creates an <code>integer<\/code> vector consisting of 100 elements, the integers from 1 to 100.<\/p>\n<pre><code>lapply(1:100, function(i) { ... } )\n<\/code><\/pre>\n<p>Executes the function given in the second argument once for each element of the first argument, passing the element as the first argument of the function when it is called for that particular element. In this case, the result is the function will be executed 100 times, passing integers 1 to 100 as the argument to the <code>i<\/code> parameter for each respective invocation. <code>lapply()<\/code> always returns the return value of every single execution of the function, combined into a <code>list<\/code> object.<\/p>\n<pre><code>unlist( ... )\n<\/code><\/pre>\n<p>Converts the <code>list<\/code> into a single homogenous (non-list) object. How this works depends on the exact nature of its argument, but in this case, it will combine the vectors returned by every function invocation into a single vector.<\/p>\n<pre><code>input[-1]\n<\/code><\/pre>\n<p>Returns the entire vector <code>input<\/code> excluding its first element.<\/p>\n<pre><code>input[1]\n<\/code><\/pre>\n<p>Returns the first element of the vector <code>input<\/code>.<\/p>\n<pre><code>c(input[-1],input[1])\n<\/code><\/pre>\n<p>Combines the two previous values. The end result is the first element has been moved to the end of the vector.<\/p>\n<pre><code>cbind(input, teach)\n<\/code><\/pre>\n<p>Performs a &#8220;column-bind&#8221; of the two aforementioned vectors. This means each vector will be treated as an <code>length(vector)<\/code>-by-1 matrix, and the two matrices will be combined into a <code>length(vector)<\/code>-by-2 matrix. (If the lengths weren&#8217;t equal, the function would still succeed, but it would recycle any short vector inputs and print a warning message.)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Could you please help me to understand an R code? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First of all, in general, var &lt;- expr evaluates the R expression expr and assigns the result to the variable var. If the statement occurs inside a function, then var becomes a function-local variable, otherwise, it becomes a global variable. c(0,0,0,1,0,1,0,1,1,1,1,0) Combines 12 double literals into a double vector in the order given. matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, &#8230; <a title=\"[Solved] Could you please help me to understand an R code?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\" aria-label=\"More on [Solved] Could you please help me to understand an R code?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[4520,413,1874,1331,321],"class_list":["post-26124","post","type-post","status-publish","format-standard","hentry","category-solved","tag-cbind","tag-function","tag-lapply","tag-matrix","tag-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Could you please help me to understand an R code? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Could you please help me to understand an R code? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] First of all, in general, var &lt;- expr evaluates the R expression expr and assigns the result to the variable var. If the statement occurs inside a function, then var becomes a function-local variable, otherwise, it becomes a global variable. c(0,0,0,1,0,1,0,1,1,1,1,0) Combines 12 double literals into a double vector in the order given. matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-15T15:23:18+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Could you please help me to understand an R code?\",\"datePublished\":\"2022-12-15T15:23:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\"},\"wordCount\":479,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"cbind\",\"function\",\"lapply\",\"matrix\",\"r\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\",\"name\":\"[Solved] Could you please help me to understand an R code? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-15T15:23:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Could you please help me to understand an R code?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Could you please help me to understand an R code? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Could you please help me to understand an R code? - JassWeb","og_description":"[ad_1] First of all, in general, var &lt;- expr evaluates the R expression expr and assigns the result to the variable var. If the statement occurs inside a function, then var becomes a function-local variable, otherwise, it becomes a global variable. c(0,0,0,1,0,1,0,1,1,1,1,0) Combines 12 double literals into a double vector in the order given. matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/","og_site_name":"JassWeb","article_published_time":"2022-12-15T15:23:18+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Could you please help me to understand an R code?","datePublished":"2022-12-15T15:23:18+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/"},"wordCount":479,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["cbind","function","lapply","matrix","r"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/","url":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/","name":"[Solved] Could you please help me to understand an R code? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-15T15:23:18+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-could-you-please-help-me-to-understand-an-r-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Could you please help me to understand an R code?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26124"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26124\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}