{"id":28,"date":"2026-03-03T06:42:29","date_gmt":"2026-03-03T06:42:29","guid":{"rendered":"https:\/\/gigz.pk\/r\/?post_type=lesson&#038;p=28"},"modified":"2026-04-01T10:40:01","modified_gmt":"2026-04-01T10:40:01","slug":"vectors-and-lists","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/r\/lesson\/vectors-and-lists\/","title":{"rendered":"Vectors and Lists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Vectors and lists are fundamental data structures in R. They help you organize, store, and manipulate data efficiently. Understanding how to use vectors and lists is essential for data analysis and statistical computing.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. Vectors in R<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">A vector is a sequence of elements of the same data type (numeric, character, or logical). Vectors are one-dimensional and are widely used for storing data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Vectors<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can create a vector using the <code>c()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers &lt;- c(1, 2, 3, 4, 5)         # Numeric vector<br>names &lt;- c(\"Alice\", \"Bob\", \"Charlie\") # Character vector<br>logical_values &lt;- c(TRUE, FALSE, TRUE) # Logical vector<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Vector Elements<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use square brackets <code>[]<\/code> to access elements:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers[1]       # Returns 1 (first element)<br>names[2]         # Returns \"Bob\" (second element)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Vector Operations<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">R allows arithmetic operations on vectors directly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a &lt;- c(1, 2, 3)<br>b &lt;- c(4, 5, 6)<br>a + b   # Returns c(5, 7, 9)<br>a * b   # Returns c(4, 10, 18)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use functions like <code>sum()<\/code>, <code>mean()<\/code>, <code>length()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sum(a)      # 6<br>mean(b)     # 5<br>length(a)   # 3<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. Lists in R<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">A list is a more flexible data structure than a vector. Lists can contain elements of different data types, including numbers, strings, vectors, and even other lists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Lists<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">my_list &lt;- list(<br>  name = \"Alice\",<br>  age = 25,<br>  scores = c(90, 85, 88),<br>  passed = TRUE<br>)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing List Elements<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>$<\/code> or double square brackets <code>[[]]<\/code> to access elements:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">my_list$name       # Returns \"Alice\"<br>my_list[[\"age\"]]   # Returns 25<br>my_list$scores[2]  # Returns 85<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Modifying Lists<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can add or update elements easily:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">my_list$city &lt;- \"New York\"   # Adds a new element<br>my_list$age &lt;- 26            # Updates age<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Differences Between Vectors and Lists<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Vectors can only contain elements of the same type; lists can hold mixed types.<\/li>\n\n\n\n<li>Vectors are one-dimensional; lists can contain other lists or complex structures.<\/li>\n\n\n\n<li>Lists are ideal for storing structured data, while vectors are best for numerical or homogeneous data.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors and lists are core data structures in R. Vectors are simple, one-dimensional sequences of the same type, while lists are flexible containers for mixed data types. Mastering these will help you efficiently organize, analyze, and manipulate data in R.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/r\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">R Programming (R Lang) > Data Types and Structures > Vectors and Lists<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1775039908915\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":5,"template":"","class_list":["post-28","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Vectors and Lists - Analyze Deep. Visualize Better. Build with R.<\/title>\n<meta name=\"description\" content=\"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Vectors and Lists - Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"og:description\" content=\"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-01T10:40:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/lesson\\\/vectors-and-lists\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Vectors and Lists - Analyze Deep. Visualize Better. Build with R.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\"},\"datePublished\":\"2026-03-03T06:42:29+00:00\",\"dateModified\":\"2026-04-01T10:40:01+00:00\",\"description\":\"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R Programming (R Lang) > Data Types and Structures > Vectors and Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\",\"name\":\"Analyze Deep. Visualize Better. Build with R.\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/r\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Vectors and Lists - Analyze Deep. Visualize Better. Build with R.","description":"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Vectors and Lists - Analyze Deep. Visualize Better. Build with R.","og_description":"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Analyze Deep. Visualize Better. Build with R.","article_modified_time":"2026-04-01T10:40:01+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/r\/lesson\/vectors-and-lists\/","url":"https:\/\/gigz.pk\/","name":"Vectors and Lists - Analyze Deep. Visualize Better. Build with R.","isPartOf":{"@id":"https:\/\/gigz.pk\/r\/#website"},"datePublished":"2026-03-03T06:42:29+00:00","dateModified":"2026-04-01T10:40:01+00:00","description":"Learn vectors and lists in R with differences and examples for beginners. Master creation, access, operations, and when to use each.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/r\/"},{"@type":"ListItem","position":2,"name":"R Programming (R Lang) > Data Types and Structures > Vectors and Lists"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/r\/#website","url":"https:\/\/gigz.pk\/r\/","name":"Analyze Deep. Visualize Better. Build with R.","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/r\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/media?parent=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}