{"id":28,"date":"2022-04-19T01:53:42","date_gmt":"2022-04-19T01:53:42","guid":{"rendered":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/chapter\/chapter-4-matrices_dataframes\/"},"modified":"2026-03-16T14:30:57","modified_gmt":"2026-03-16T14:30:57","slug":"chapter-4-matrices_dataframes","status":"publish","type":"chapter","link":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/chapter\/chapter-4-matrices_dataframes\/","title":{"raw":"Chapter 4 - Matrices and Dataframes","rendered":"Chapter 4 &#8211; Matrices and Dataframes"},"content":{"raw":"In this chapter, we are going to move away from one-dimensional data such as vectors and start working with two-dimensional or tabular data such as matrices and dataframes or datasets.\n<div class=\"textbox textbox--learning-objectives\"><header class=\"textbox__header\">\n<p class=\"textbox__title\">Learning Objectives<\/p>\n\n<\/header>\n<div class=\"textbox__content\">\n\nIn this chapter, we will cover the following topics:\n<ul>\n \t<li>how to create a matrix;<\/li>\n \t<li>how to add row and column names to a matrix;<\/li>\n \t<li>how to add a new column to a matrix;<\/li>\n \t<li>how to create a dataframe;<\/li>\n \t<li>how to install libraries or packages;<\/li>\n \t<li>how to load libraries or packages;<\/li>\n \t<li>three different ways to load datasets.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h1>Part 1 - Creating matrices and dataframes<\/h1>\nIn this first part, you are going to learn about matrices and dataframes, and their differences, by creating them with simple code. We will examine how to add names to the rows and columns.\n\n[embed]https:\/\/vimeo.com\/714855278[\/embed]\n<h1>Parts 2 &amp; 3 - Installing and loading libraries and loading datasets<\/h1>\nIn order to work with dataframes, we have to know where to find them and how to load them in our environment. The video below explains first how to install and load new libraries and packages, to extend the basic R functions. And then, we will look at three different ways of loading datasets into our environment.\n\n[embed]https:\/\/vimeo.com\/715745876[\/embed]\n\nNow that we know how to load datasets in our environment, we are ready to start exploring and extracting information from our data. In the next chapter, we will examine simple ways of extracting basic information.\n<div class=\"textbox textbox--key-takeaways\"><header class=\"textbox__header\">\n<p class=\"textbox__title\">Key functions used in this chapter<\/p>\n\n<\/header>\n<div class=\"textbox__content\">\n\nPart 1\n<ul>\n \t<li><strong>class()<\/strong>: the function that prints the class of an object (such as a matrix);<\/li>\n \t<li><strong>dim()<\/strong>: the function that prints the dimensions of an object (such as number of rows and columns);<\/li>\n \t<li><strong>matrix()<\/strong>: the function that creates a matrix;<\/li>\n \t<li><strong>rownames()<\/strong>: the function that creates names for the rows of a matrix or dataframe;<\/li>\n \t<li><strong>colnames()<\/strong>: the functions that creates names for the columns of a matrix or dataframe;<\/li>\n \t<li><strong>cbind()<\/strong>: the function that appends a new column to a matrix or dataframe;<\/li>\n \t<li><strong>data.frame()<\/strong>: the function that creates a dataframe.<\/li>\n<\/ul>\nPart 2\n<ul>\n \t<li><strong>install.packages()<\/strong>: the function that installs a new library or package in R Studio;<\/li>\n \t<li><strong>library()<\/strong>: the function that loads a library;<\/li>\n<\/ul>\nPart 3\n<ul>\n \t<li><strong>data()<\/strong>: the function that loads a dataset from a loaded library or package;<\/li>\n \t<li><strong>read.csv()<\/strong>: the function that reads a csv file into the environment.<\/li>\n<\/ul>\n<\/div>\n<\/div>\nCheck your comprehension of this chapter's material by taking the quiz below.\n\nThe original version of this chapter contained H5P content. You may want to remove or replace this element.","rendered":"<p>In this chapter, we are going to move away from one-dimensional data such as vectors and start working with two-dimensional or tabular data such as matrices and dataframes or datasets.<\/p>\n<div class=\"textbox textbox--learning-objectives\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Learning Objectives<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p>In this chapter, we will cover the following topics:<\/p>\n<ul>\n<li>how to create a matrix;<\/li>\n<li>how to add row and column names to a matrix;<\/li>\n<li>how to add a new column to a matrix;<\/li>\n<li>how to create a dataframe;<\/li>\n<li>how to install libraries or packages;<\/li>\n<li>how to load libraries or packages;<\/li>\n<li>three different ways to load datasets.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h1>Part 1 &#8211; Creating matrices and dataframes<\/h1>\n<p>In this first part, you are going to learn about matrices and dataframes, and their differences, by creating them with simple code. We will examine how to add names to the rows and columns.<\/p>\n<p><iframe loading=\"lazy\" id=\"oembed-1\" title=\"Chapter 4 - Part 1 - Matrices and Dataframes\" src=\"https:\/\/player.vimeo.com\/video\/714855278?dnt=1&amp;app_id=122963\" width=\"500\" height=\"281\" frameborder=\"0\"><\/iframe><\/p>\n<h1>Parts 2 &amp; 3 &#8211; Installing and loading libraries and loading datasets<\/h1>\n<p>In order to work with dataframes, we have to know where to find them and how to load them in our environment. The video below explains first how to install and load new libraries and packages, to extend the basic R functions. And then, we will look at three different ways of loading datasets into our environment.<\/p>\n<p><iframe loading=\"lazy\" id=\"oembed-2\" title=\"Chapter 4 - Part 2 - Matrices and Dataframes\" src=\"https:\/\/player.vimeo.com\/video\/715745876?dnt=1&amp;app_id=122963\" width=\"500\" height=\"305\" frameborder=\"0\"><\/iframe><\/p>\n<p>Now that we know how to load datasets in our environment, we are ready to start exploring and extracting information from our data. In the next chapter, we will examine simple ways of extracting basic information.<\/p>\n<div class=\"textbox textbox--key-takeaways\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Key functions used in this chapter<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p>Part 1<\/p>\n<ul>\n<li><strong>class()<\/strong>: the function that prints the class of an object (such as a matrix);<\/li>\n<li><strong>dim()<\/strong>: the function that prints the dimensions of an object (such as number of rows and columns);<\/li>\n<li><strong>matrix()<\/strong>: the function that creates a matrix;<\/li>\n<li><strong>rownames()<\/strong>: the function that creates names for the rows of a matrix or dataframe;<\/li>\n<li><strong>colnames()<\/strong>: the functions that creates names for the columns of a matrix or dataframe;<\/li>\n<li><strong>cbind()<\/strong>: the function that appends a new column to a matrix or dataframe;<\/li>\n<li><strong>data.frame()<\/strong>: the function that creates a dataframe.<\/li>\n<\/ul>\n<p>Part 2<\/p>\n<ul>\n<li><strong>install.packages()<\/strong>: the function that installs a new library or package in R Studio;<\/li>\n<li><strong>library()<\/strong>: the function that loads a library;<\/li>\n<\/ul>\n<p>Part 3<\/p>\n<ul>\n<li><strong>data()<\/strong>: the function that loads a dataset from a loaded library or package;<\/li>\n<li><strong>read.csv()<\/strong>: the function that reads a csv file into the environment.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<p>Check your comprehension of this chapter&#8217;s material by taking the quiz below.<\/p>\n<p>The original version of this chapter contained H5P content. You may want to remove or replace this element.<\/p>\n","protected":false},"author":1,"menu_order":4,"template":"","meta":{"pb_show_title":"","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[49],"contributor":[],"license":[],"class_list":["post-28","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":18,"_links":{"self":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapters\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapters\/28\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapters\/28\/revisions\/29"}],"part":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/parts\/18"}],"metadata":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapters\/28\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/pressbooks\/v2\/chapter-type?post=28"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/wp\/v2\/contributor?post=28"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/introduction2r\/wp-json\/wp\/v2\/license?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}