{"id":169,"date":"2022-10-20T12:51:13","date_gmt":"2022-10-20T12:51:13","guid":{"rendered":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/?page_id=169"},"modified":"2022-10-20T12:52:38","modified_gmt":"2022-10-20T12:52:38","slug":"r-variables","status":"publish","type":"page","link":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/r-variables\/","title":{"rendered":"R&nbsp;Variables"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Variables in R<\/h2>\n\n\n\n<p>Variables are containers for storing data values.<\/p>\n\n\n\n<p>R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it. To assign a value to a variable, use the&nbsp;<code>&lt;-<\/code>&nbsp;sign. To output (or print) the variable value, just type the variable name:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name &lt;- \"John\"\nage &lt;- 40\n\nname   # output \"John\"\nage    # output 40<\/code><\/pre>\n\n\n\n<p>From the example above,&nbsp;<code>name<\/code>&nbsp;and&nbsp;<code>age<\/code>&nbsp;are&nbsp;<strong>variables<\/strong>, while&nbsp;<code>\"John\"<\/code>&nbsp;and&nbsp;<code>40<\/code>&nbsp;are&nbsp;<strong>values<\/strong>.<\/p>\n\n\n\n<p>In other programming language, it is common to use&nbsp;<code>=<\/code>&nbsp;as an assignment operator. In R, we can use both&nbsp;<code>=<\/code>&nbsp;and&nbsp;<code>&lt;-<\/code>&nbsp;as assignment operators.<\/p>\n\n\n\n<p>However,&nbsp;<code>&lt;-<\/code>&nbsp;is preferred in most cases because the&nbsp;<code>=<\/code>&nbsp;operator can be forbidden in some context in R.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Print \/ Output Variables<\/h2>\n\n\n\n<p>Compared to many other programming languages, you do not have to use a function to print\/output variables in R. You can just type the name of the variable:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name &lt;- \"John Doe\"\n\nname # auto-print the value of the name variable<\/code><\/pre>\n\n\n\n<p>However, R does have a&nbsp;<code>print()<\/code>&nbsp;function available if you want to use it. This might be useful if you are familiar with other programming languages, such as&nbsp;<a href=\"https:\/\/www.w3schools.com\/python\/default.asp\">Python<\/a>, which often use a&nbsp;<code>print()<\/code>&nbsp;function to output variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name &lt;- \"John Doe\"\n\nprint(name) # print the value of the name variable<\/code><\/pre>\n\n\n\n<p>And there are times you must use the&nbsp;<code>print()<\/code>&nbsp;function to output code, for example when working with&nbsp;<code>for<\/code>&nbsp;loops (which you will learn more about in a later chapter):<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (x in 1:10) {\n  print(x)\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating Variables in R Variables are containers for storing data values. R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it. To assign a value to a&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/r-variables\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-169","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/pages\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":2,"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/pages\/169\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/pages\/169\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/dorsu.edu.ph\/faculty\/jscabrera\/wp-json\/wp\/v2\/media?parent=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}