{"id":75139,"date":"2020-01-07T14:28:20","date_gmt":"2020-01-07T08:58:20","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75139"},"modified":"2020-01-07T14:28:20","modified_gmt":"2020-01-07T08:58:20","slug":"r-vector","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-vector\/","title":{"rendered":"R Vector &#8211; How to Create, Combine and Index Vectors in R?"},"content":{"rendered":"<p><strong>In this TechVidvan tutorial, you\u2019ll learn about vector in R programming. You\u2019ll learn to create, combine, and index vectors in R.<\/strong><\/p>\n<p>Vectors are the simplest data structures in R. They are sequences of elements of the same basic type.<\/p>\n<p>These types can be numeric, integer, complex, character, and logical. In R, the more complicated data structures are made with vectors as building-blocks.<\/p>\n<p>In this article, you will get the answer to all these questions in detail:<\/p>\n<ul>\n<li><a class=\"tv-anchor-link\" href=\"#create-vector\">How to create vector in R?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#types-of-vector\">What are the types of vectors in R?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#find-type-of-R-vector\">How to find the type of R vector?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#combine-R-vectors\">How to combine R vectors?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#coercion\">What is coercion in R vector?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#index\">How to access elements of R Vector?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#vectorarithmetic\">What are some basic vector arithmetic in R?<\/a><\/li>\n<li><a class=\"tv-anchor-link\" href=\"#vector-function\">What is vector function in R?<\/a><\/li>\n<\/ul>\n<p>So without further ado, let\u2019s begin!<\/p>\n<h2 id=\"create-vector\">How to create vector in R?<\/h2>\n<p>There are numerous ways to create an R vector:<\/p>\n<h3>1. Using c() Function<\/h3>\n<p>To create a vector, we use the <strong><code>c()<\/code><\/strong> function:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec &lt;- c(1,2,3,4,5) #creates a vector named vec\n&gt; vec #prints the vector vec<\/pre>\n<p><strong>Output<\/strong>:<br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vector-using-c.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75209\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vector-using-c.png\" alt=\"vectors - create vector using c()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>2. Using assign() function<\/h3>\n<p>Another way to create a vector is the <strong><code>assign()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; assign(\"vec2\",c(6,7,8,9,10)) #creates a vector named vec2\n&gt; vec2                         #prints the vector vec2<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vector-using-assign.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75212\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vector-using-assign.png\" alt=\"create R vector using assign() function\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>3. Using : operator<\/h3>\n<p>An easy way to make integer vectors is to use the<strong> <code>:<\/code><\/strong>\u00a0operator.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec3 &lt;- 1:20\n&gt; vec3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vectors-using-_-operator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75213\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-create-vectors-using-_-operator.png\" alt=\"create R vectors using : operator\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3 id=\"types-of-vector\">What are the types of vectors in R?<\/span><\/h3>\n<p>A vector can be of different types depending on the elements it contains. These may be:<\/p>\n<h4>1. Numeric Vectors<\/h4>\n<p>Vectors containing numeric values.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; num_vec &lt;- c(1,2,3,4,5)\n&gt; num_vec<\/pre>\n<h4>2. Integer Vectors<\/h4>\n<p>Vectors containing integer values.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; int_vec &lt;- c(6L,7L,8L,9L,10L)\n&gt; int_vec<\/pre>\n<h4>3. Logical Vectors<\/h4>\n<p>Vectors containing logical values of TRUE or FALSE.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; log_vec &lt;- c(TRUE,FALSE,TRUE,FALSE,FALSE)\n&gt; log_vec<\/pre>\n<h4>4. Character Vectors<\/h4>\n<p>Vectors containing text.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; char_vec &lt;- c(\"aa\",\"bb\",\"cc\",\"dd\",\"ee\")\n&gt; char_vec<\/pre>\n<h4>5. Complex Vectors<\/h4>\n<p>Vectors containing complex values.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; comp_vec &lt;- c(12+1i,3i,5+4i,4+9i,6i)\n&gt; comp_vec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/R-vectors-types-of-vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75227\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/R-vectors-types-of-vectors.png\" alt=\"R vectors types of vectors\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3 id=\"find-type-of-R-vector\">How to find the type of R vector?<\/h3>\n<p>We can use the <strong><code>typeof()<\/code><\/strong> function to find the type of a vector. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; typeof(num_vec)\n&gt; typeof(int_vec)\n&gt; typeof(log_vec)\n&gt; typeof(char_vec)\n&gt; typeof(comp_vec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-typeof-function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75215\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-typeof-function.png\" alt=\"R-vectors-typeof()-function\" width=\"1299\" height=\"740\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: The <strong><code>typeof()<\/code><\/strong> function returns \u201cdouble\u201d for numeric values. This is because of the way numeric-class stores a value. The numeric class stores values as double-precision floating-point numbers. Their type is double while their class is numeric.<\/p>\n<h3 id=\"combine-R-vectors\">How to combine R vectors?<\/h3>\n<p>The <strong><code>c()<\/code><\/strong> function can also combine two or more vectors and add elements to vectors.<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec4 &lt;- c(vec, vec2)\n&gt; vec4<\/pre>\n<p><strong>Example 2<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec5 &lt;- c(vec4,4,55,vec)\n&gt; vec5<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-combining-vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75216\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-combining-vectors.png\" alt=\"combining R vectors\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3 id='coercion'>What is coercion in R vector?<\/h3>\n<p>Vectors only hold elements of the same data type. If there is more than one <a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-types\/\"><em><strong>data type<\/strong><\/em><\/a>, the <strong><code>c()<\/code><\/strong> function converts the elements. This is known as coercion. The conversion takes place from lower to higher types.<\/p>\n<p style=\"text-align: center\"><strong>logical &lt; integer &lt; double &lt; complex &lt; character.<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec6 &lt;- c(1,FALSE,3L,12+5i,\"hello\")\n&gt; typeof(vec6)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-coercion.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75217\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-coercion.png\" alt=\"coercion in R vector\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3 id='index'>How to access elements of R vector?<\/h3>\n<p>We use vector indexing to access the elements of a vector. We can select or omit elements of a vector, by appending an index vector in square brackets to the name of the vector.<\/p>\n<p>There are four types of index vectors:<\/p>\n<ol>\n<li>Logical index vector<\/li>\n<li>Positive-integral index vector<\/li>\n<li>Negative-integral index vector<\/li>\n<li>Character index vector<\/li>\n<\/ol>\n<p>Let us look at these different indexing techniques:<\/p>\n<h4>1. Logical index vectors<\/h4>\n<p>We can use a vector of logical values to index another vector of the same length. R includes the elements corresponding to TRUE in the index vector and omits the elements corresponding to FALSE. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; logind_vec &lt;- vec[c(FALSE,TRUE,FALSE,TRUE,TRUE)]\n&gt; logind_vec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-logical-indexing-logind_vec.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75218\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-logical-indexing-logind_vec.png\" alt=\"R vectors logical indexing\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>Instead of using logical vectors of equal length, we can also use a logical condition. This includes the elements which satisfy the condition while removes those who don\u2019t.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; logind_vec2 &lt;- vec3[(vec3%%3)==0] #only allow those divisible by 3\n&gt; logind_vec2<\/pre>\n<p><strong>Output<\/strong> :<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-logical-indexing-logind_vec2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75219\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-logical-indexing-logind_vec2.png\" alt=\"vectors logical indexing\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>2. Positive-integral index vectors<\/h4>\n<p>Vector indices in R start from 1. We can use positive integers to select specific elements. We can also use vectors of positive integers to include multiple specific elements while leaving others.<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; posint_vec &lt;- vec[3]\n&gt; posint_vec<\/pre>\n<p><strong>Example 2<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; posint_vec2 &lt;- vec4[c(2,3,6,7,9)]\n&gt; posint_vec2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-positive-integral-indexing-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75220\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-positive-integral-indexing-.png\" alt=\"R vectors positive integral indexing\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>3. Negative-integral index vectors<\/h4>\n<p>In negative integral indexing, negative integers denote the elements to be excluded. When using negative integral vectors, R removes the denoted elements and returns the remaining as the result.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; negint_vec &lt;- vec[c(-2,-4)]\n&gt; negint_vec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-negative-integral-indexing.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75221\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-negative-integral-indexing.png\" alt=\"negative integral indexing in R vectors \" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>4. Character indexing vectors<\/h4>\n<p>We use a character vector, only to index vectors with a \u2018names\u2019 attribute. Character vector indexing is useful when dealing with named <a href=\"https:\/\/en.wikipedia.org\/wiki\/Vector-R\">R vectors<\/a>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; named_vec &lt;- c(\"first\"=1,\"second\"=2,\"third\"=3,\"fourth\"=4)\n&gt; named_vec\n&gt; charind_vec &lt;- named_vec[c(\"first\",\"second\",\"fourth\")] \n&gt; charind_vec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-character-index-vector.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75222\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-character-index-vector.png\" alt=\"character index in R vectors \" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3 id='vectorarithmetic'>What are some basic vector arithmetic in R?<\/h3>\n<p>R performs arithmetic operations on vectors memberwise. This means that the operations are performed on each member. For example:<\/p>\n<p><strong>Multiplication:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; multivec &lt;- vec*2\n&gt; multivec<\/pre>\n<p><strong>Addition:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec_plus_three &lt;- vec+3\n&gt; vec_plus_three<\/pre>\n<p><strong>Subtraction:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec_min_one &lt;- vec-1\n&gt; vec_min_one<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-multivecvecplusthree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75223\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-multivecvecplusthree.png\" alt=\"R vector arithmetic multivec \" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can also perform an arithmetic operation like an addition of two vectors of <strong>equal length<\/strong>. This adds the corresponding members in the two vectors. For example:<\/p>\n<p><strong>Addition of vectors<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vector_add &lt;- vec+vec2 #vec = 1,2,3,4,5 vec2 = 6,7,8,9,10\n&gt; vector_add<\/pre>\n<p><strong>Multiplication of vector<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vector_mul &lt;- vec*vec2\n&gt; vector_mul<\/pre>\n<p><strong>Subtraction of vector<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vector_sub &lt;- vec2-vec\n&gt; vector_sub<\/pre>\n<p><strong>Division of vector<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vector_div &lt;- multivec\/vec # multivec = 2,4,6,8,10\n&gt; vector_div<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-addsubmuldiv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75224\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-addsubmuldiv.png\" alt=\"R vectors arithmetic add,sub,mul,div\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>If the two vectors are of <strong>unequal length<\/strong>, the shorter one will be recycled to match the longer vector.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; recycle_vec &lt;- vec*vec4\n&gt; recycle_vec<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-recycling.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75225\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-arithmetic-recycling.png\" alt=\"arithmetic recycling in R vector\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: Here, the first vector <em>vec<\/em> has five elements. The second vector <em>vec4<\/em> has ten elements. Therefore, the first vector is cycled twice to match the second.<\/p>\n<h3 id='vector-function'>What is vector function in R?<\/h3>\n<p>R has many functions that can manipulate vectors or get more information about them. Here are some of the commonly used functions:<\/p>\n<p>1.<strong>\u00a0<code>seq()<\/code>&#8211;<\/strong> seq() function generates regular numeric sequences. The function has the following arguments:<\/p>\n<ul>\n<li>from: starting value<\/li>\n<li>to: ending value<\/li>\n<li>by: increment (default is 1)<\/li>\n<li>length.out: length of the sequence<\/li>\n<li>along.with: the length of this argument can define the length of the sequence.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec_seq &lt;- seq(from=1,to=20,length=30)\n&gt; vec_seq<\/pre>\n<p>2. <strong><code>rep()<\/code><\/strong> &#8211; The rep() function repeats a given numeric vector. The function has the following arguments:<\/p>\n<ul>\n<li>X: x is the numeric vector that is repeated.<\/li>\n<li>times: number of repetitions.<\/li>\n<li>each: number of repetitions for each element of the vector.<\/li>\n<li>length.out: the length of the resultant vector. The function repeats until it reaches the length.<\/li>\n<\/ul>\n<p><strong>Code<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; vec_rep &lt;- rep(c(2,3,4), times=3)\n&gt; vec_rep<\/pre>\n<p>3. <strong><code>sum()<\/code><\/strong> &#8211; The sum() function returns an integer value which is the sum of all the elements in a vector.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; sum(vec_rep)<\/pre>\n<p>4. <strong>Type checking and conversion functions<\/strong> &#8211; the functions as.numeric() \/ as.character() \/ as.logical() \/ as.integer() can convert a vector into their corresponding type. The functions is.numeric() \/is.character() \/is.logical() etc. tell whether the vector is of the corresponding type or not.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; is.numeric(vec_rep)\n&gt; as.character(vec_rep)<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-functions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75226\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/vectors-vector-functions.png\" alt=\"vector functions in R\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Vectors are one of the basic data structures of R. They are sequences of values of the same data type. We can classify them as one-dimensional, homogeneous data structures.<\/p>\n<p>Don&#8217;t know much about inbuilt data structures in R? Then have a look at basic <em><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-structures\/\">data structures in R<\/a> <\/strong><\/em>with examples.<\/p>\n<p>In this tutorial, we learned about R vectors. We learned how to create vectors and how many types of vectors there are. We also looked at coercion and the various indexing techniques for vectors.<\/p>\n<p>Finally, we explored the mechanics of vector arithmetic in R and a few functions that can generate and manipulate vectors.<\/p>\n<p>If you like our content, then please give your feedback in the comment section.<\/p>\n<p>Keep learning!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this TechVidvan tutorial, you\u2019ll learn about vector in R programming. You\u2019ll learn to create, combine, and index vectors in R. Vectors are the simplest data structures in R. They are sequences of elements&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75231,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1255,1256,1257,1258,1254,1259,1260,1261],"class_list":["post-75139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-atomic-vector-in-r","tag-boolean-vector-in-r","tag-create-vector-in-r","tag-making-a-vector-in-r","tag-r-vector","tag-r-vector-operations","tag-vector-function-in-r","tag-vector-in-r-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn everything about R vectors from creating, combining, and indexing R vectors and also explore vector arithmetics and vector functions 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:\/\/techvidvan.com\/tutorials\/r-vector\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn everything about R vectors from creating, combining, and indexing R vectors and also explore vector arithmetics and vector functions in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-vector\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-07T08:58:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.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=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan","description":"Learn everything about R vectors from creating, combining, and indexing R vectors and also explore vector arithmetics and vector functions 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:\/\/techvidvan.com\/tutorials\/r-vector\/","og_locale":"en_US","og_type":"article","og_title":"R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan","og_description":"Learn everything about R vectors from creating, combining, and indexing R vectors and also explore vector arithmetics and vector functions in R.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-vector\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-07T08:58:20+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"R Vector &#8211; How to Create, Combine and Index Vectors in R?","datePublished":"2020-01-07T08:58:20+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/"},"wordCount":1085,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.jpg","keywords":["atomic vector in R","boolean vector in R","create vector in R","making a vector in R","R Vector","R vector operations","vector function in r","vector in r programming"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-vector\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/","url":"https:\/\/techvidvan.com\/tutorials\/r-vector\/","name":"R Vector - How to Create, Combine and Index Vectors in R? - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.jpg","datePublished":"2020-01-07T08:58:20+00:00","description":"Learn everything about R vectors from creating, combining, and indexing R vectors and also explore vector arithmetics and vector functions in R.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-vector\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/vectors-in-R.jpg","width":802,"height":420,"caption":"r vectors"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-vector\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"R Vector &#8211; How to Create, Combine and Index Vectors in R?"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75139"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75139\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75231"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}