{"id":1762,"date":"2020-12-05T16:14:01","date_gmt":"2020-12-05T16:14:01","guid":{"rendered":"https:\/\/ded9.com\/?p=1762"},"modified":"2025-12-13T12:03:15","modified_gmt":"2025-12-13T12:03:15","slug":"learn-lists-in-r-in-plain-language-lists-are-objects","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/learn-lists-in-r-in-plain-language-lists-are-objects\/","title":{"rendered":"Learn Lists in R in Plain Language: Understanding R Lists"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Lists in R: Lists are objects in R that contain elements of different types, such as numbers, strings, vectors, and other lists within them. A list can include a matrix or a function as its elements.\u00a0<\/span><\/p>\n<p>In R, Lists are created using the List () function.<span id=\"more-56191\"><\/span><\/p>\n<h2><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258607 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/lists-in-R.jpg\" alt=\"In R, Lists are created using the List () function.\" width=\"802\" height=\"420\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/lists-in-R.jpg 802w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/lists-in-R-300x157.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/lists-in-R-768x402.jpg 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/h2>\n<h2>Create Lists In R<\/h2>\n<p>The following is an example of how to create a list of logical strings, numbers, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Value_(ethics)\" target=\"_blank\" rel=\"noopener\">values<\/a>.<\/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;}\"># Create a list containing strings, numbers, vectors and a logical\r\n\r\n# values.\r\n\r\nlist_data &lt;- list (\u201cRed\u201d, \u201cGreen\u201d, c (21,32,11), TRUE, 51.23, 119.1)\r\n\r\nprint (list_data)<\/pre>\n<\/div>\n<p>When we run the above code, the following results are generated:<\/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]]\r\n\r\n[1] \u201cRed\u201d\r\n\r\n[[2]]\r\n\r\n[1] \u201cGreen\u201d\r\n\r\n[[3]]\r\n\r\n[1] 21 32 11\r\n\r\n[[4]]\r\n\r\n[1] TRUE\r\n\r\n[[5]]\r\n\r\n[1] 51.23\r\n\r\n[[6]]\r\n\r\n[1] 119.1<\/pre>\n<\/div>\n<h1><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"aligncenter wp-image-258613 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/R-list-1.jpg\" alt=\"The following is an example of how to create a list of logical strings, numbers, and values.\" width=\"832\" height=\"466\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/R-list-1.jpg 832w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/R-list-1-300x168.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/R-list-1-768x430.jpg 768w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/span><\/h1>\n<h2><span style=\"font-size: 18pt;\">Name The Members of the List<\/span><\/h2>\n<p>List members can be named and accessed by name.<\/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;}\"># Create a list containing a vector, a matrix and a list.\r\n\r\nlist_data &lt;- list (c (\"Jan\", \"Feb\", \"Mar\"), matrix (c (3,9,5,1, -2,8), nrow = 2),\r\n\r\nlist (\u201cgreen\u201d, 12.3))\r\n\r\n# Give names to the elements in the list.\r\n\r\nnames (list_data) &lt;- c (\u201c1st Quarter\u201d, \u201cA_Matrix\u201d, \u201cA Inner list\u201d)\r\n\r\n# Show the list.\r\n\r\nprint (list_data)<\/pre>\n<\/div>\n<p>When we run the above code, the following result is created:<\/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;}\">$ `1st_Quarter`\r\n\r\n[1] \u201cJan\u201d \u201cFeb\u201d \u201cMar\u201d\r\n\r\n$ A_Matrix\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 3 5 -2\r\n\r\n[2,] 9 1 8\r\n\r\n$ A_Inner_list\r\n\r\n$ A_Inner_list [[1]]\r\n\r\n[1] \u201cgreen\u201d\r\n\r\n$ A_Inner_list [[2]]\r\n\r\n[1] 12.3<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Access the elements of a list<\/span><\/h2>\n<p>They can be accessed via the List&#8217;s member index. Named lists can be accessed by their names.<\/p>\n<p>We use the List in the example above; We continue:<\/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;}\"># Create a list containing a vector, a matrix and a list.\r\n\r\nlist_data &lt;- list (c (\"Jan\", \"Feb\", \"Mar\"), matrix (c (3,9,5,1, -2,8), nrow = 2),\r\n\r\nlist (\u201cgreen\u201d, 12.3))\r\n\r\n# Give names to the elements in the list.\r\n\r\nnames (list_data) &lt;- c (\u201c1st Quarter\u201d, \u201cA_Matrix\u201d, \u201cA Inner list\u201d)\r\n\r\n# Access the first element of the list.\r\n\r\nprint (list_data [1])\r\n\r\n# Access the thrid element. As it is also a list, all its elements will be printed.\r\n\r\nprint (list_data [3])\r\n\r\n# Access the list element using the name of the element.\r\n\r\nprint (list_data $ A_Matrix)<\/pre>\n<\/div>\n<p>When we run the above <a href=\"https:\/\/ded9.com\/top-7-code-sharing-sites-for-developers\/\">code<\/a>, 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;}\">$ `1st_Quarter`\r\n\r\n[1] \u201cJan\u201d \u201cFeb\u201d \u201cMar\u201d\r\n\r\n$ A_Inner_list\r\n\r\n$ A_Inner_list [[1]]\r\n\r\n[1] \u201cgreen\u201d\r\n\r\n$ A_Inner_list [[2]]\r\n\r\n[1] 12.3\r\n\r\n[, 1] [, 2] [, 3]\r\n\r\n[1,] 3 5 -2\r\n\r\n[2,] 9 1 8<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Hire members from a list<\/span><\/h2>\n<p>We can list the elements as shown below: Add or remove. We can only add elements to the bottom of the List. Additionally, we can only delete elements from the bottom of a list. But we can update any aspect.<\/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;}\"># Create a list containing a vector, a matrix and a list.\r\n\r\nlist_data &lt;- list (c (\"Jan\", \"Feb\", \"Mar\"), matrix (c (3,9,5,1, -2,8), nrow = 2),\r\n\r\nlist (\u201cgreen\u201d, 12.3))\r\n\r\n# Give names to the elements in the list.\r\n\r\nnames (list_data) &lt;- c (\u201c1st Quarter\u201d, \u201cA_Matrix\u201d, \u201cA Inner list\u201d)\r\n\r\n# Add element at the end of the list.\r\n\r\nlist_data [4] &lt;- \u201cNew element\u201d\r\n\r\nprint (list_data [4])\r\n\r\n# Remove the last element.\r\n\r\nlist_data [4] &lt;- NULL\r\n\r\n# Print the 4th Element.\r\n\r\nprint (list_data [4])\r\n\r\n# Update the 3rd Element.\r\n\r\nlist_data [3] &lt;- \u201cupdated element\u201d\r\n\r\nprint (list_data [3])<\/pre>\n<\/div>\n<p>When we execute 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]]\r\n\r\n[1] \u201cNew element\u201d\r\n\r\n$ &lt;NA&gt;\r\n\r\nNULL\r\n\r\n$ `A Inner list`\r\n\r\n[1] \u201cupdated element\u201d<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Merge Lists In R<\/span><\/h2>\n<p>You can merge multiple lists into a single list. This is achieved by placing all the lists within a list() 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;}\"># Create two lists.\r\n\r\nlist1 &lt;- list (1,2,3)\r\n\r\nlist2 &lt;- list (\"Sun\", \"Mon\", \"Tue\")\r\n\r\n# Merge the two lists.\r\n\r\nmerged.list &lt;- c (list1, list2)\r\n\r\n# Print the merged list.\r\n\r\nprint (merged.list)<\/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]]\r\n\r\n[1] 1\r\n\r\n[[2]]\r\n\r\n[1] \u06f2\r\n\r\n[[3]]\r\n\r\n[1] 3\r\n\r\n[[4]]\r\n\r\n[1] \u201cSun\u201d\r\n\r\n[[5]]\r\n\r\n[1] \u201cMon\u201d\r\n\r\n[[6]]\r\n\r\n[1] \u201cTue\u201d<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Convert the List to a vector<\/span><\/h2>\n<p>A list can be converted into an array, enabling its elements to be used for subsequent processing. All arithmetic operations on vectors can be performed after the List has been converted to vectors.<\/p>\n<p>To perform this conversion, we use the <strong>unlist<\/strong><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><strong>()<\/strong> function<\/span>. This function takes a List as input and generates a vector.<\/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;}\"># Create lists.\r\n\r\nlist1 &lt;- list (1: 5)\r\n\r\nprint (list1)\r\n\r\nlist2 &lt;-list (10:14)\r\n\r\nprint (list2)\r\n\r\n# Convert the lists to vectors.\r\n\r\nv1 &lt;- unlist (list1)\r\n\r\nv2 &lt;- unlist (list2)\r\n\r\nprint (v1)\r\n\r\nprint (v2)\r\n\r\n# Now add the vectors\r\n\r\nresult &lt;- v1 + v2\r\n\r\nprint (result)<\/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]]\r\n\r\n[1] 1 2 3 4 5\r\n\r\n[[1]]\r\n\r\n[1] 10 11 12 13 14\r\n\r\n[1] 1 2 3 4 5\r\n\r\n[1] 10 11 12 13 14\r\n\r\n[1] 11 13 15 17 19<\/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 list in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A list in R is a flexible data structure that can hold elements of different types (vectors, numbers, strings, other lists).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I create a list in R?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the list() function with elements inside to define a list.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why are lists useful in R programming?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Lists allow you to group and manage complex or mixed data types in a single variable for analysis.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Lists in R: Lists are objects in R that contain elements of different types, such as numbers, strings, vectors, and other lists within them. A list can include a matrix or a function as its elements.\u00a0 In R, Lists are created using the List () function. Create Lists In R The following is an example [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1763,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[3324],"class_list":["post-1762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-r"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=1762"}],"version-history":[{"count":4,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1762\/revisions"}],"predecessor-version":[{"id":266104,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/1762\/revisions\/266104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/1763"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=1762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=1762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=1762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}