

{"id":2911,"date":"2017-06-19T12:37:40","date_gmt":"2017-06-19T07:07:40","guid":{"rendered":"http:\/\/data-flair.training\/blogs\/?p=2911"},"modified":"2024-05-03T15:39:20","modified_gmt":"2024-05-03T10:09:20","slug":"r-data-types","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-data-types\/","title":{"rendered":"R Data Types &#8211; Become an expert in its implementation!"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1282,&quot;href&quot;:&quot;https:\\\/\\\/www.r-project.org&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Moving ahead in our R DataFlair Tutorial Series, today we will learn all about R data types in which we will understand about numeric, integer, logical, complex, and character values in R programming in detail. We will also look at the differences amongst them and how to create variables containing these types of values.<\/p>\n<p>So, let\u2019s start with the tutorial.<\/p>\n<h2>What are Data types in R?<\/h2>\n<p>For correct processing, a programming language must know what can and cannot be done to a particular value. For example, addition cannot be performed on the words \u2018hello\u2019 and \u2018world\u2019.<\/p>\n<p>Similarly, you cannot change the numbers 1 and -34.5 from lower to uppercase. Due to this, R has a feature called the data types. Different kinds of values are assigned different data types that help differentiate them. These types have certain characteristics and rules associated with them that define their properties.<\/p>\n<p>Choosing the appropriate data type for variables and data structures is essential for efficient memory usage and code performance. In R, selecting the most suitable data type based on the nature of the data and the operations to be performed can lead to more readable and maintainable code.<\/p>\n<p>R provides the class() and typeof() functions to find out what is the class and type of any variable. R has five data types which are:<\/p>\n<ol>\n<li>Numeric<\/li>\n<li>Integers<\/li>\n<li>Complex<\/li>\n<li>Logical<\/li>\n<li>Characters<\/li>\n<\/ol>\n<p>Let\u2019s go through these data types one-by-one.<\/p>\n<p>For practice, you can <a href=\"https:\/\/data-flair.training\/blogs\/how-to-install-r\/\"><strong><em>install R &amp; RStudio<\/em><\/strong><\/a> by following our step by step R installation tutorial.<\/p>\n<h3>1. Numeric Data Type<\/h3>\n<p>The numeric data type is for numeric values. It is the default data type for numbers in R.<br \/>\nExamples of numeric values would be 1, 34.5, 3.145, -24, -45.003, etc.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; num &lt;- 1\r\n&gt; class(num)\r\n&gt; typeof(num)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74230\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1.png\" alt=\"numeric data type in R\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-numerics1-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Note:<\/strong> When R stores a number in a variable, it converts the number into a \u2018double\u2019 value or a decimal type with at least two decimal places. This means that a value such as \u20191\u2019 is stored as 1.00 with a type of double and a class of numeric.<\/p>\n<p>You can check the <a href=\"https:\/\/data-flair.training\/blogs\/r-numeric-and-character-functions\/\"><em><strong>Numeric and Character Functions in R<\/strong> <\/em><\/a>for better understanding.<\/p>\n<h3>2. Integers Data Type<\/h3>\n<p>The Integer data type is used for integer values. To store a value as an integer, we need to specify it as such. The integer data type is commonly used for discrete only values like unique ids. We can store as well as convert a value into an integer type using the as.integer() function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; int &lt;- as.integer(16)\r\n&gt; class(int)\r\n&gt; typeof(int)\r\n&gt; int2 &lt;- as.integer(num)\r\n&gt; int2\r\n&gt; class(int2)\r\n&gt; typeof(int2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74231\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122.png\" alt=\"integer - R data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integers122-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong>We can also use the capital \u2018L\u2019 notation to denote that a particular value is of the integer data type.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; int3 &lt;- 5L\r\n&gt; class(int3)\r\n&gt; typeof(int3)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74232\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3.png\" alt=\"R data types integer\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-integer3-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>3. Complex Data Type<\/h3>\n<p>The complex data type is to store numbers with an imaginary component. Examples of complex values would be 1+2i, 3i, 4-5i, -12+6i, etc.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; comp &lt;- 22-6i\r\n&gt; class(comp)\r\n&gt; typeof(comp)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74233\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4.png\" alt=\"complex - R data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-complex4-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h3>4. Logical Data Type<\/h3>\n<p>The logical data type stores logical or boolean values of TRUE or FALSE.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; logi &lt;- FALSE\r\n&gt; class(logi)\r\n&gt; typeof(logi)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74234\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5.png\" alt=\"logical - R data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-logical5-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h3>5. Character Data Type<\/h3>\n<p>The character data type stores character values or strings. Strings in R can contain the alphabet, numbers, and symbols. The easiest way to denote that a value is of character type in R is to wrap the value inside single or double inverted commas.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; char &lt;- \"dataflair1234\"\r\n&gt; class(char)\r\n&gt; typeof(char)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74235\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6.png\" alt=\"character - R data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-characters6-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<p>We can also use the <em>as.character()<\/em> function to store a value as a character or to convert a value to the character data type.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; char2 &lt;- as.character(\"hello\")\r\n&gt; char3 &lt;- as.character(comp)\r\n&gt; char2\r\n&gt; char3\r\n&gt; class(char2)\r\n&gt; typeof(char2)\r\n&gt; class(char3)\r\n&gt; typeof(char3)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74236\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237.png\" alt=\"chatacter - R data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-character237-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>Converting Data Types in R<\/h3>\n<p>We can convert values from one data type to another (if possible). <a href=\"https:\/\/www.r-project.org\/\">R<\/a> has certain rules that govern these conversions.<\/p>\n<p>In some programming languages, data type conversion can occur implicitly (automatically) by the language itself, while in others, it must be explicitly specified by the programmer. R generally requires explicit type conversion, ensuring transparency and control over data transformations.<\/p>\n<h4>Conversion into Numeric<\/h4>\n<p>We can use the <em>as.numerical<\/em> function to convert the values of other data types into numerical values. The conversion follows a few rules, which are:<\/p>\n<ul>\n<li>To convert an integer value into a numeric, we can use the <em>as.numeric<\/em> function.<\/li>\n<li>We can convert a complex value into numeric by using the function. This removes the imaginary part of the number.<\/li>\n<li>Logical values can be converted into numeric as well by the function. The TRUE value is converted to 1, and FALSE is converted to 0.<\/li>\n<li>Character values can similarly be converted into numerical values but if the string contains letters, alphabets, and symbols then the numeric value becomes NA.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; num2 &lt;- as.numeric(int)\r\n&gt; num2\r\n&gt; num3 &lt;- as.numeric(comp)\r\n&gt; num3\r\n&gt; num4 &lt;- as.numeric(logi)\r\n&gt; num4\r\n&gt; num5 &lt;- as.numeric(char)\r\n&gt; num5 &lt;- as.numeric(\"1234\")\r\n&gt; num5<\/pre>\n<p><strong>Output:<\/strong><strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74238\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8.png\" alt=\"R data types convert to numeric\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-numeric8-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h4>Conversion into Integer<\/h4>\n<p>The<em> as.integer<\/em> function can convert the values of other data types into integer values according to the following rules:<\/p>\n<ul>\n<li>Numeric values can be converted into an integer using the function. This removes any decimal values from the number.<\/li>\n<li>Complex values can also be converted into integers. The function removes the imaginary parts of the number.<\/li>\n<li>The conversion from logical values to integers is similar to the conversion of logical values to numerics. TRUE is converted to 1, and FALSE is converted to 0.<\/li>\n<li>Character values can be converted into integers as well by using the <em>as.integer<\/em> function. This conversion follows the same rules as the character to numeric conversion.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; int4 &lt;- as.integer(num)\r\n&gt; int4\r\n&gt; int5 &lt;- as.integer(14.7)\r\n&gt; int5\r\n&gt; int6 &lt;- as.integer(comp)\r\n&gt; int6\r\n&gt; int7 &lt;- as.integer(logi)\r\n&gt; int7\r\n&gt; int8 &lt;- as.integer(\"1234\")\r\n&gt; int8<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74239\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9.png\" alt=\"convert R to integer\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-integer9-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h4>Conversion into Complex<\/h4>\n<p>Using the <em>as.complex<\/em> function, we can convert other values into the complex data types. The conversion takes place according to the following rules:<\/p>\n<ul>\n<li>Numeric values can be converted into complex by using the as.complex function or by adding an imaginary part to it.<\/li>\n<li>Integer value can also be converted into complex values similarly.<\/li>\n<li>Logical values become 0+0i for FALSE and 1+0i for TRUE when converted into complex values using the as.complex function. We can also convert a logical value into a complex value by adding an imaginary part to it.<\/li>\n<li>The conversion from a character to a complex is the same as the conversion from character to numeric or an integer with 0i added to the converted value if it is not NA.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; comp2 &lt;- as.complex(num)\r\n&gt; comp2\r\n&gt; comp3 &lt;- as.complex(int)\r\n&gt; comp3\r\n&gt; comp4 &lt;- as.complex(logi)\r\n&gt; comp4\r\n&gt; comp5 &lt;- as.complex(\"1234\")\r\n&gt; comp5<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74240\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10.png\" alt=\"convert-to-complex data types\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types-convert-to-complex10-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h4>Conversion into Logical<\/h4>\n<p>Conversion into logical data type can be done by using the <em>as.logical<\/em> function, by following the given rules:<\/p>\n<ul>\n<li>Numeric, integer, and complex values can be converted into logical values, but the function returns FALSE if the value is zero and TRUE if it is anything else.<\/li>\n<li>Character values when converted by the as.logical function, always return NA.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; logi2 &lt;- as.logical(num)\r\n&gt; logi2\r\n&gt; logi3 &lt;- as.logical(int)\r\n&gt; logi3\r\n&gt; logi4 &lt;- as.logical(comp)\r\n&gt; logi4\r\n&gt; logi5 &lt;- as.logical(char)\r\n&gt; logi5<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74241\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11.png\" alt=\"data types convert to logical \" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-logical11-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><\/p>\n<h4>Conversion into character<\/h4>\n<p>We can convert a value of any data type into character data type using the <em>as.character<\/em> function. The function converts the original value into a character string.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; char2 &lt;- as.character(num)\r\n&gt; char2\r\n&gt; char3 &lt;- as.character(int)\r\n&gt; char3\r\n&gt; char4 &lt;- as.character(comp)\r\n&gt; char4\r\n&gt; char5 &lt;- as.character(logi)\r\n&gt; char5<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74242\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12.png\" alt=\"convert data types to character\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12-768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12-1024x584.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/r-data-types-convert-to-character12-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/strong><strong>Note:<\/strong> NA values have no type and therefore stay as such when converted to any type.<\/p>\n<h2>Summary<\/h2>\n<p>In this R tutorial, we learned about the various types of data that R supports. These data types are the basic building blocks of any kind of data in R programming. They assign meaning to values in R and also tell R how to process and how not to process them.<\/p>\n<p>Now, you must go through our next tutorial on <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/data-structures-in-r\/\">data structures in R\u00a0<\/a><\/strong><\/em>to learn how to use these data types together.<\/p>\n<p>Still, if you have any doubts related to the R data types tutorial, do let us know by leaving a comment below. We will be happy to solve them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Moving ahead in our R DataFlair Tutorial Series, today we will learn all about R data types in which we will understand about numeric, integer, logical, complex, and character values in R programming in&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":74251,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[21656,21658,21660,3493,21657,21655,21659,11176],"class_list":["post-2911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-atomic-data-types-in-r","tag-check-data-type-in-r","tag-convert-character-to-numeric-in-r","tag-data-types-in-r","tag-data-types-in-r-programming","tag-r-boolean","tag-r-data-type-conversion","tag-r-data-types"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Data Types - Become an expert in its implementation! - DataFlair<\/title>\n<meta name=\"description\" content=\"In this R data types tutorial, learn about vectors, matrices, lists, data frames &amp; factors, along with converting one data type values to another in R.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/r-data-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Data Types - Become an expert in its implementation! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this R data types tutorial, learn about vectors, matrices, lists, data frames &amp; factors, along with converting one data type values to another in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-data-types\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-19T07:07:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-03T10:09:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Data Types - Become an expert in its implementation! - DataFlair","description":"In this R data types tutorial, learn about vectors, matrices, lists, data frames & factors, along with converting one data type values to another in R.","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:\/\/data-flair.training\/blogs\/r-data-types\/","og_locale":"en_US","og_type":"article","og_title":"R Data Types - Become an expert in its implementation! - DataFlair","og_description":"In this R data types tutorial, learn about vectors, matrices, lists, data frames & factors, along with converting one data type values to another in R.","og_url":"https:\/\/data-flair.training\/blogs\/r-data-types\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-06-19T07:07:40+00:00","article_modified_time":"2024-05-03T10:09:20+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"R Data Types &#8211; Become an expert in its implementation!","datePublished":"2017-06-19T07:07:40+00:00","dateModified":"2024-05-03T10:09:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/"},"wordCount":1197,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg","keywords":["atomic data types in r","check data type in r","convert character to numeric in r","Data types in R","data types in r programming","r boolean","R data type conversion","R Data Types"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-data-types\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/","url":"https:\/\/data-flair.training\/blogs\/r-data-types\/","name":"R Data Types - Become an expert in its implementation! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg","datePublished":"2017-06-19T07:07:40+00:00","dateModified":"2024-05-03T10:09:20+00:00","description":"In this R data types tutorial, learn about vectors, matrices, lists, data frames & factors, along with converting one data type values to another in R.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-data-types\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/R-data-types.jpg","width":802,"height":420,"caption":"R data types"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-data-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"R Data Types &#8211; Become an expert in its implementation!"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=2911"}],"version-history":[{"count":18,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2911\/revisions"}],"predecessor-version":[{"id":136736,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2911\/revisions\/136736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/74251"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=2911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=2911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=2911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}