{"id":1793,"date":"2020-12-05T17:15:47","date_gmt":"2020-12-05T17:15:47","guid":{"rendered":"https:\/\/ded9.com\/?p=1793"},"modified":"2025-12-13T11:58:08","modified_gmt":"2025-12-13T11:58:08","slug":"teaching-r-variables-in-simple-language-a-variable-provides-us-with-a-named-space","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/teaching-r-variables-in-simple-language-a-variable-provides-us-with-a-named-space\/","title":{"rendered":"Teaching R Variables in Simple Language: What a Variable Is"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">R Variables: A variable provides a named space for storing values. A variable in R can be stored in a vector or group of vectors, or it can be combined with many <a href=\"https:\/\/en.wikipedia.org\/wiki\/R_(programming_language)\" target=\"_blank\" rel=\"noopener\">R<\/a> objects.\u00a0<\/span><\/p>\n<p>An allowable name for the variable includes letters, numbers, dots, and characters underlined. A variable name begins with a letter or a dot followed by no numbers.<span id=\"more-56167\"><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Variable name<\/strong><\/td>\n<td><strong>Being allowed<\/strong><\/td>\n<td><strong>The reason<\/strong><\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">var_name2.<\/p>\n<\/td>\n<td>Allowed<\/td>\n<td>Includes letters, numbers, dots, and underlines<\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">var_name%<\/p>\n<\/td>\n<td>illegal<\/td>\n<td>It is not permitted due to its presence (%), as only the dot symbol is permitted.<\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">\u06f2var_name<\/p>\n<\/td>\n<td>illegal<\/td>\n<td>Because it starts with a number.<\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">var_name<\/p>\n<p dir=\"ltr\">var. name<\/p>\n<\/td>\n<td>Allowed<\/td>\n<td>The variable name can start with a dot (.) But it should not be followed by a number.<\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">.2var_name<\/p>\n<\/td>\n<td>illegal<\/td>\n<td>Following the name&#8217;s starting point is a number that renders it unauthorized.<\/td>\n<\/tr>\n<tr>\n<td>\n<p dir=\"ltr\">_var_name<\/p>\n<\/td>\n<td>illegal<\/td>\n<td>Started with _ is not allowed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258586 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/Data_Types_in_R_Programming.webp\" alt=\"Teaching R Variables In Simple Language\" width=\"850\" height=\"450\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/Data_Types_in_R_Programming.webp 850w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/Data_Types_in_R_Programming-300x159.webp 300w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/Data_Types_in_R_Programming-768x407.webp 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/span><\/h1>\n<h2><span style=\"font-size: 18pt;\">Variable allocation<\/span><\/h2>\n<p>Variables can be used using the left part, the right part, or equal; Assigned to the operator. The values \u200b\u200bof the variables can be printed using the print() or cat() functions. The cat () function concatenates several items into a single line of continuous output.<\/p>\n<blockquote>\n<p dir=\"ltr\"># Assignment using equal operator.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">var.1 = c (0,1,2,3)<\/pre>\n<\/div>\n<p dir=\"ltr\"># Assignment using leftward operator.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">var.2 &lt;- c (\u201clearn\u201d, \u201cR\u201d)<\/pre>\n<\/div>\n<p dir=\"ltr\"># Assignment using rightward operator.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">c (TRUE, 1) -&gt; var.3\r\n\r\nprint (var.1)\r\n\r\ncat(\"var.1 is\", var.1, \"\\n\")\r\ncat(\"var.2 is\", var.2, \"\\n\")\r\ncat(\"var.3 is\", var.3, \"\\n\")<\/pre>\n<\/div>\n<\/blockquote>\n<p>When the above code is executed, the following output is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] 0123\r\n\r\nvar.1 is 0123\r\n\r\nvar.2 is learn R\r\n\r\nvar.3 is 1 1<\/pre>\n<\/div>\n<p><strong>Note<\/strong>\u00a0&#8211; Vector (c (TRUE, 1) contains a combination of logic and numeric classes. Therefore, the logic class is corrected to a numeric class and returns TRUE as 1.<\/p>\n<h2><span style=\"font-size: 18pt;\">One-variable data types<\/span><\/h2>\n<p>In the<a href=\"https:\/\/ded9.com\/the-best-programming-languages-for-each-field-everything-you-need-to-know-before-starting-work\/\"> programming language<\/a> R, A variable in itself is not expressed from any data type; Instead, it receives the data type of the object R to which it is assigned.<\/p>\n<p>R is a dynamically typed language, meaning that when using a variable in the program, we can change its data type repeatedly.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">var_x &lt;- \u201cHello\u201d\r\n\r\ncat (\u201cThe class of var_x is\u201c, class (var_x), \u201d\\ n\u201d)\r\n\r\nvar_x &lt;- 34.5\r\n\r\ncat (\u201dNow the class of var_x is\u201c, class (var_x), \u201d\\ n\u201d)\r\n\r\nvar_x &lt;- 27L\r\n\r\ncat (\u201dNext the class of var_x becomes\u201c, class (var_x), \u201d\\ n\u201d)<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<p dir=\"ltr\">The class of var_x is a character<\/p>\n<p dir=\"ltr\">Now the class of var_x is numeric<\/p>\n<p dir=\"ltr\">Next, the class of var_x becomes an integer<\/p>\n<h2>Find Variables<\/h2>\n<p>To identify the variables that are currently available in the workspace, we use the Is () function. The Is () function can also use patterns to match the names of variables.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">print (ls ())<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \u201cmy var\u201d \u201cmy_new_var\u201d \u201cmy_var\u201d \u201cvar.1\u201d\r\n\r\n[5] \"var.2\" \"var.3\" \"var.name\" \"var_name2.\"\r\n\r\n[9] \"var_x\" \"varname\"<\/pre>\n<\/div>\n<p><strong>Note: This is a sample output,<\/strong>\u00a0depending on the variables defined in your environment.<\/p>\n<p>This function uses patterns to match variable names.<\/p>\n<blockquote>\n<p dir=\"ltr\"># List the variables starting with the pattern \u201cvar\u201d.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">print (ls (pattern = \u201cvar\u201d))<\/pre>\n<\/div>\n<\/blockquote>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \"my var\" \"my_new_var\" \"my_var\" \"var.1\"\r\n\r\n[5] \"var.2\" \"var.3\" \"var.name\" \"var_name2.\"\r\n\r\n[9] \"var_x\" \"varname\"<\/pre>\n<\/div>\n<p>Variables starting with (.) They are hidden (Hayden). They can be listed using the &#8220;all.names = TRUE&#8221; argument to the Is() function.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">print (ls (all.name = TRUE)<\/pre>\n<\/div>\n<p>When we run the above code,\u00a0 the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \u201c.cars\u201d \u201c.Random.seed\u201d \u201c.var_name\u201d \u201c.varname\u201d \u201c.varname2\u201d\r\n\r\n[6] \u201cmy var\u201d \u201cmy_new_var\u201d \u201cmy_var\u201d \u201cvar.1\u201d \u201cvar.2\u201d\r\n\r\n[11] \u201dvar.3 \u2033\u201c var.name \u201d\u201c var_name2. \u201cVar_x\"<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Clear variables<\/span><\/h2>\n<p>Variables can be cleared using the rm () function. Next, we present variable var.3. We clean it. An error will occur in the variable value print section.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">rm (var.3)\r\n\r\nprint (var.3)<\/pre>\n<\/div>\n<p>When the above code is executed, the result is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">[1] \"var.3\"\r\n\r\nError in print (var.3): object 'var.3' not found<\/pre>\n<\/div>\n<p>Can be used with the rm() and is () functions; it clears all variables.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">rm (list = ls ())\r\n\r\nprint (ls ())<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">character (0)<\/pre>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a variable in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A variable in R is a named storage location that holds data values used in programming.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you assign a value to a variable in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You assign a value using the &lt;- or = operator to link the name with the data.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why are variables important in R programming?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Variables allow you to store, manipulate, and reuse data throughout your code efficiently.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>R Variables: A variable provides a named space for storing values. A variable in R can be stored in a vector or group of vectors, or it can be combined with many R objects.\u00a0 An allowable name for the variable includes letters, numbers, dots, and characters underlined. A variable name begins with a letter or [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1794,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[11894],"class_list":["post-1793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-variables"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=1793"}],"version-history":[{"count":4,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1793\/revisions"}],"predecessor-version":[{"id":266102,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1793\/revisions\/266102"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/1794"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=1793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=1793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=1793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}