{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 03 - R Essentials\n", "\n", "Marina Adshade, Paul Corcuera, Giulia Lo Forte, Jane Platt \n", "2024-05-29\n", "\n", "## Prerequisites\n", "\n", "1. Understand how to effectively use R scripts or create Jupyter cells.\n", "\n", "## Learning Outcomes\n", "\n", "1. Understand objects, variables, and functions in R.\n", "\n", "## 3.0 Setting Up\n", "\n", "Run the code cell below before starting!" ], "id": "4372dba4-279f-471c-aa4a-6143e3522e4c" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "library(IRdisplay)" ], "id": "e7bcf151-3be8-47c2-b625-37a18d641b2f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.1 Basics of Using R\n", "\n", "In this notebook, we will be introduced to **R**. R is a programming\n", "language which is particularly well-suited for statistics, econometrics,\n", "and data science. If you are familiar with other programming languages\n", "such as Python, this will likely be very familiar. If this is your first\n", "time working with a programming language, don’t be intimidated! Try to\n", "play around with the examples as you work through this notebook; it’s\n", "easiest to learn R (or any programming language) by playing around with\n", "it.\n", "\n", "R is an object oriented programming language. This means that we can\n", "create many different things (e.g. data sets, matrices, vectors,\n", "scalars) within it and they will all be stored in the same environment\n", "and accessed the same way.\n", "\n", "Every new line in R is understood as\n", "\n", "``` r\n", "function_name(input1 = valid_alternatives, input2 = valid_alternatives, ... ) \n", "```\n", "\n", "Fairly simple stuff! However, we first need to understand the basic data\n", "types that exist in R and how we can put these into different\n", "objects/data structures. Usually we use functions that are provided in a\n", "given `library` (package), but later we’ll also look at how to create\n", "our own functions.\n", "\n", "## 3.2 Basic Data Types\n", "\n", "To begin, it’s important to get a good grasp of the different **data\n", "types** in R and how to use them. Whenever we work with R, we will be\n", "manipulating different kinds of information referred to as “data”. Data\n", "comes in many different forms, called *types*, which define how we can\n", "use it in calculations or visualizations in R.\n", "\n", "R has 6 basic data types. Data types are used to store information about\n", "a variable or object in R:\n", "\n", "1. **Character**: data in text format, like “word” or “abc”;\n", "2. **Numeric** (real or decimal): data in real number format, like 6 or\n", " 18.8 (referred to as **Double** or **dbl** in R);\n", "3. **Integer**: data in whole number (integer) format, like 2L (the L\n", " tells R to store this as an integer);\n", "4. **Logical**: truth values, like TRUE or FALSE;\n", "5. **Complex**: data in complex (i.e. imaginary) format, like 1+6i\n", " (where $i$ is the $\\sqrt{-1}$);\n", "6. **Raw**: raw digital data, which is unusual and which will not be\n", " covered in this section.\n", "\n", "If we are ever wondering what kind of type an object in R has, or what\n", "its properties are, we can use the following two functions that allow us\n", "to examine the data type and elements contained within an object:\n", "\n", "- `typeof()`: this function returns a character string that\n", " corresponds to the data type of an object;\n", "- `str()`: this function displays a compact internal structure of an R\n", " object.\n", "\n", "We will see some examples of these in just a moment.\n", "\n", "## 3.3 Data Structures\n", "\n", "Basic data is fine, but we often need to store data in more complex\n", "forms. Luckily, data can be stored in different structures in R beyond\n", "these basic data types. Below are some of the most important data\n", "structures in R, each of which we will look at individually in greater\n", "detail.\n", "\n", "- **Vectors**: a vector of values, like $(1,3,5,7)$;\n", "- **Matrices**: a matrix of values, like $[1,2; 3,4]$ (usually\n", " displayed as a square);\n", "- **Lists**: a list of elements with named properties, like\n", " $(pet =''cat'', ''dog'', ''mouse'')$;\n", "- **Data frames**: a collection of vectors or lists, organized into\n", " rows and columns according to observations.\n", "\n", "Note that vectors don’t need to be numeric! We can also use some useful\n", "built-in functions to create data structures (we don’t have to create\n", "our own functions to do so).\n", "\n", "- `c`: this function combines values into a vector;\n", "- `matrix`: this function creates a matrix from a given set of values;\n", "- `list`: this function creates a list from a given set of values;\n", "- `data.frame`: this function creates a data frame from a given set of\n", " lists or vectors.\n", "\n", "Let’s look at each of these four data structures in turn.\n", "\n", "### 3.3.1 Vectors\n", "\n", "Vectors are important, and we can work with them by creating them from\n", "values or other elements using the `c()` function:" ], "id": "4a6e2da4-1969-43b9-94db-f00a4635716b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# generate a vector containing values\n", "z <- c(1, 2, 3)\n", "\n", "# generate a vector containing characters\n", "countries <- c(\"Canada\", \"Japan\", \"United Kingdom\")" ], "id": "227c8e0f-bf9c-4917-af14-14be894ba849" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also **access** the elements of a vector. Since a vector is made\n", "of basic data, we can access its elements using the `[ ]` index\n", "notation. This is very similar to how we refer to elements of a vector\n", "in mathematical notation.\n", "\n", "**Note**: If you’re familiar with other programming languages, it’s\n", "important to note that R is 1-indexed. So, the first element of a vector\n", "is 1, not 0. Keep this in mind!\n", "\n", "Below we access specific components of the *z* and *countries* vectors\n", "that have already been defined." ], "id": "3c00b0e0-35c7-48ef-9fc3-b48ab210aab3" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# the 2nd component of z\n", "z[2]\n", "\n", "# the 2nd component of countries\n", "countries[2]" ], "id": "57ee70c1-a6bf-4822-9002-52410d2e539c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "As mentioned above, we can use the `typeof` and `str` functions to\n", "glimpse the kind of data stored in our objects.\n", "\n", "Run the cell below to see how this works:" ], "id": "05e9edaf-5801-4326-931c-f77c77770675" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# view the data type of countries\n", "typeof(countries)\n", "\n", "# view the data structure of countries\n", "str(countries)\n", "\n", "# view the data type of z\n", "typeof(z)\n", "\n", "# view the data structure of z\n", "str(z)" ], "id": "fb5cd28c-ec16-4ddc-b64b-dea773d10206" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output of `str(countries)` begins by acknowledging that the\n", "contained data is of a character (chr) type. The information contained\n", "in the `[1:3]` first refers to the component number (there is only 1\n", "component list here) and then the number of observations (the 3\n", "countries).\n", "\n", "### 3.3.2 Matrices\n", "\n", "Just like vectors, we can also create matrices; we can think of matrices\n", "as organized collections of row (or column) vectors. They’re a little\n", "bit more complicated to create manually since we need to use a more\n", "complex function: `matrix`. However, the simplest way to create them is\n", "just to provide a vector of all the values to this function, then tell R\n", "how the matrix should be organized. R will then fill in the specified\n", "values. An example is below." ], "id": "4b63e545-cf51-4f70-bee3-e849be5fcbf4" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# generate a 2 x 2 matrix\n", "m <- matrix(c(2,3,6,7,7,3), nrow=2,ncol=3)\n", "\n", "print(m)" ], "id": "9a053737-6f41-452d-9822-469f1240a98d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take note of the order in which the values are filled in; it might be\n", "unexpected to you!\n", "\n", "Just like with vectors, we can also access parts of a matrix. If we look\n", "at the cell output above, we can see some notation like `[1,]` and\n", "`[,2]`. These are the *rows* and *columns* of the matrix. We can refer\n", "to them using this notation. We can also refer to specific elements\n", "using `[1,2]`. Again, this is very similar to the mathematical notation\n", "for matrices. Below we access specific columns, rows, and elements of\n", "the matrix *m*." ], "id": "990dab19-3a5d-4316-8991-e949f043a257" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 2nd column of matrix\n", "m[,2] \n", "\n", "# 1st row of matrix\n", "m[1,] \n", "\n", "# Element in row 1, column 2\n", "m[1,2]" ], "id": "2171e702-894a-4ac6-bf76-1f0b515e94e5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "As with vectors, we can also observe and inspect the data structures of\n", "matrices using the helper function above." ], "id": "3ec6d166-3bf5-47f3-b8d6-a15367c1fe82" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# what type is m?\n", "typeof(m)\n", "\n", "# glimpse data structure of m\n", "str(m)" ], "id": "03e53c00-1fba-4c4d-84de-a8aa663d2b1c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output of `str(m)` begins by displaying that the data in the matrix\n", "is of a numeric (num) type. The `[1:2, 1:3]` shows the structure of the\n", "rows and columns. The final part displays the values in the matrix.\n", "\n", "### 3.3.3 Lists\n", "\n", "Lists are a little bit more complex because they can store many\n", "different data types and objects, each of which can be given *names*\n", "which are specific ways to refer to these objects. Names can be any\n", "useful descriptive term for an element of a list. You can think of lists\n", "as flexible vectors with names. Let’s generate a list below." ], "id": "12351435-b7d3-4cac-97db-7db054f1d4c8" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# generate a list with 3 components named \"text\" \"a_vector\" and \"a_matrix\"\n", "my_list <- list(text=\"test\", a_vector = z, a_matrix = m) " ], "id": "8bed5307-c5f8-43db-b5b8-9c434fd58ad4" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access elements of the list using the `[ ]` or `[[ ]]`\n", "operations. There is a difference:\n", "\n", "- `[ ]` accesses the *elements of the list*: the name and object;\n", "- `[[ ]]` accesses the *object* directly.\n", "\n", "We usually want to use `[[ ]]` when working with data stored in lists.\n", "One very nice feature of lists is that we can refer to their elements by\n", "number (like a vector) or by their name. Let’s access specific\n", "components of the list both by name and number below." ], "id": "0fbb16a9-6616-413c-ba2d-46d788f75d76" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 1st component in list\n", "my_list[[1]] \n", "\n", "#1st component in list by name (text)\n", "my_list[[\"text\"]]\n", "\n", "# 1st part of the list (note the brackets)\n", "my_list[1] \n", "\n", "# glimpse data type of my_list\n", "typeof(my_list)" ], "id": "e9ec8b5f-1483-4d67-95dc-3a0d37208330" }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is one final way to access elements of a list by name: using the\n", "`$` or **access** operator. This works basically like `[[name]]` but is\n", "more transparent when writing code. We write down the object we want to\n", "access, followed by the operator, followed by the property. Let’s do\n", "this below." ], "id": "4491ba3d-96f7-48a2-8359-5b9c559b1369" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get the named property \"text\"\n", "my_list$text\n", "\n", "#get the name property\n", "my_list$a_matrix" ], "id": "7d5b3cdd-4a2c-406a-905b-acdfa855eaab" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that this *only* works for named objects. This is\n", "particularly convenient for data frames, which we will discuss next.\n", "\n", "### 3.3.4 Data frames\n", "\n", "Data frames are the most complex object we will work with in this\n", "module, but also the most important. They represent data - like the kind\n", "of data we use in econometrics. In this module, we will primarily focus\n", "on *tidy* data, data in which the columns represent variables and the\n", "rows represent observations. In terms of R, we can think of data frames\n", "as a combination of a matrix and a list. Let’s generate a data frame\n", "below using the `data.frame` function." ], "id": "60911e84-47c1-4c1e-9ffc-2b97a452c56f" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# generates a dataframe with 2 columns and 3 rows\n", "df <- data.frame(ID=c(1:3),\n", " Country=countries)" ], "id": "9ba8c937-ddac-438a-935e-fd1763d2895d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access specific columns (variables) of this data frame using\n", "their names or their ordering. We can also use the `str` function like\n", "before to inspect the data structure of this new data frame *df*." ], "id": "bacc9741-1452-4db9-be4d-cb7eee382faf" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If we want access specific parts of the dataframe:\n", "\n", "# 2nd column in dataframe\n", "df[2] \n", "\n", "df$Country\n", "\n", "# glimpse compact data structure of df\n", "str(df)" ], "id": "07f89208-8e02-433d-847d-1e3e7e230f11" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the `str(df)` command shows us the names of the columns in\n", "this data set, as well as how we can access them.\n", "\n", "## 3.4 Objects and Variables\n", "\n", "At this point, we have now covered some of the different types of data\n", "in R and how they work. However, let’s see how we can work with them in\n", "more detail by writing R code. A **variable** or **object** is a name\n", "assigned to a memory location in the R workspace (working memory). For\n", "now we can use the terms variable and object interchangeably. An object\n", "will always have an associated type, determined by the information\n", "assigned to it. Clear and concise object assignment is essential for\n", "**reproducible data analysis**, as mentioned in [Module\n", "2](https://comet.arts.ubc.ca/docs/Research/econ490-r/02_Working_Rscripts.html).\n", "\n", "When it comes to code, we can assign information (stored in a specific\n", "data type) to variables and objects using the **assignment operator**\n", "`<-`. With the assignment operator, the information on the right-hand\n", "side is assigned to the variable/object on the left-hand side. We’ve\n", "seen this already with some of the vectors, lists, matrices, and data\n", "frames defined earlier in this notebook.\n", "\n", "**Important Note**: R is case sensitive. When referring to an object, it\n", "must *exactly* match its assignment. `Var_1` is not the same as `var_1`\n", "or `var1`.\n", "\n", "In the example below, `\"Hello\"` has been assigned to the object `var_1`.\n", "`\"Hello\"` will be stored in the R workspace as an object named\n", "`\"var_1\"`, which we can call at any time." ], "id": "9d415331-ad62-4fb6-81f0-7f6088d15adb" }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "var_1 <- \"Hello\"\n", "\n", "var_1\n", "\n", "typeof(var_1)" ], "id": "498fd415-9437-40c7-88a5-77afe20d4260" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can create variables of many types, including all of the basic and\n", "advanced types discussed above. Below are some examples of four\n", "different type objects assigned to four different variables." ], "id": "56e0787e-9947-4eca-8a3c-4a8ec289b1ec" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var_2 <- 34.5 # numeric/double\n", "var_3 <- 6L # integer\n", "var_4 <- TRUE # logical/boolean\n", "var_5 <- 1 + 3i # complex" ], "id": "1b509fc7-69de-486b-ba20-70b7219137c5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.5 Operations\n", "\n", "In R, we can also perform **operations** on objects; the type of an\n", "object defines what operations are valid. All of the basic mathematical\n", "and logical operations we are familiar with are examples of these, but\n", "there are many more. For example:" ], "id": "88ce650f-dc38-4152-a0ce-df6602afe1ea" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a <- 4 # creates an object named \"a\" assigned to the value: 4\n", "b <- 6 # creates an object named \"b\" assigned to the value: 6\n", "c <- a + b # creates an object \"c\" assigned to the value (a = 4) + (b = 6)" ], "id": "e91af097-cc3d-4a55-9051-7a03216abfb9" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try and think about what value c holds!\n", "\n", "We can view the assigned value of *c* in two different ways:\n", "\n", "1. By printing `a + b`\n", "2. By printing `c`\n", "\n", "Run the code cell below to see for yourself!" ], "id": "4bc58814-14d6-4860-81a1-63341375020f" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + b\n", "c" ], "id": "95e30e96-d0c8-4619-858b-f5d8e3968a4a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to change the value of an object. In the example\n", "below, the object *b* has been reassigned the value 5." ], "id": "e78e999f-8a92-4c87-9ef8-b139ebfa4cf8" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b <- 5 " ], "id": "e3080738-3e91-42ea-a3d1-5e1264446e39" }, { "cell_type": "markdown", "metadata": {}, "source": [ "R will now store the updated value of 5 in the object *b*. This\n", "overrides the original assignment of 6 to *b*. The ability to change\n", "object names is a key benefit of using variables in R. We can simply\n", "reassign the value to a variable without having to change that value\n", "everywhere in our code. This will be quite useful when we want to do\n", "things such as change the name of a column in a data set in a future\n", "module.\n", "\n", "**Tip:** Remember to use a unique object name that hasn’t been used\n", "before when creating a new object. This helps to avoid unplanned object\n", "reassignment. Remember, descriptive names are better!\n", "\n", "Earlier, we discussed operations and used the example of `+` to run the\n", "addition of a and b. `+` is a type of arithmetic **operator**, meaning\n", "it is a symbol that tells R to perform a specific operation. R has 4\n", "types of operators, some of which we’ve already seen and some of which\n", "we haven’t:\n", "\n", "1. **Arithmetic operators**: used to carry out mathematical operations.\n", " Ex. `*` for multiplication, `/` for division, `^` for\n", " exponentiation, etc.;\n", "2. **Assignment operators**: used to assign values to variables. Ex.\n", " `<-`;\n", "3. **Relational operators**: used to compare between values. Ex. `>`\n", " for greater than, `==` for equal to, `!=` for not equal to etc.;\n", "4. **Logical operators**: used to carry out Boolean operations. Ex. `!`\n", " for Logical NOT, `&` for Logical AND etc.\n", "\n", "We won’t cover all of these right now, but you can look them up online.\n", "For now, keep an eye out for them when they appear.\n", "\n", "## 3.6 Functions\n", "\n", "These simple operations are great to start with, but what if we want to\n", "do operations on different values of X and Y over and over and don’t\n", "want to constantly rewrite this code? This is where **functions** come\n", "in. Functions allow us to carry out specific tasks. We simply pass in a\n", "parameter or parameters to the function. Code is then executed in the\n", "function body based on these parameters, and output may be returned.\n", "\n", "Some functions are built-in to R, such as the `library` function we have\n", "been using to load in packages. However, some functions are customized,\n", "meaning we created them ourselves. Below is the format for these\n", "customized functions that we create ourselves and customize for our\n", "intended purpose.\n", "\n", "``` r\n", "Functionname <- function(arguments)\n", " {code operating on the arguments\n", " }\n", "```\n", "\n", "This structure says that we start with a name for our function\n", "(`Functionname`) and we use the assignment operator similarly to when we\n", "assign values to variables. We then pass **arguments or parameters** to\n", "our function (which can be numeric, characters, vectors, collections\n", "such as lists, etc.) in the `(arguments)` space; think of them as the\n", "*inputs* to the function.\n", "\n", "Finally, within the curly brackets we write the code needed to\n", "accomplish our desired task. Once we have done this, we can call this\n", "function anywhere in our code (after having run the cell defining the\n", "function!) and evaluate it based on the specific parameter values that\n", "we choose to pass in as inputs.\n", "\n", "An example is shown below; can you figure out what this function does?" ], "id": "7dab71ca-b9ae-4dde-8993-7948509f978a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_function <- function(x, y)\n", " {x = x + y\n", " 2 * x\n", "}" ], "id": "361e5aa7-82cc-4651-9212-c947b8841ce3" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameters passed as input to functions can be given **defaults**.\n", "Defaults are specific values for parameters that have been chosen and\n", "defined within the circular brackets of the function definition. For\n", "example, we can define `y = 3` as a default in our `my_function`. When\n", "we call this function, we then do not have to specify an input for *y*\n", "unless we want to." ], "id": "62184a71-8660-48f5-97ba-8c45af371058" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_function <- function(x, y = 3)\n", " {x = x + y\n", " 2 * x}\n", "\n", "my_function(2)" ], "id": "95d83884-3060-47b7-b3c8-08c77068ab93" }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, if we want to override this default, we can simply call the\n", "function with a new input for *y*. This is done below for `y = 4`,\n", "allowing us to execute our code as though our default was actually\n", "`y = 4`." ], "id": "98e97187-7ec9-4b7f-ba79-f3f23812266a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_function <- function(x, y = 3)\n", " {x = x + y\n", " 2 * x}\n", "\n", "my_function(2, 4)" ], "id": "db3f1609-337a-423c-8ca0-5136a57d5a39" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, note that we can **nest** functions within functions, meaning\n", "we can call functions inside of other functions, creating very complex\n", "arrangements. Just be sure that these inner functions have themselves\n", "already been defined! An example is below." ], "id": "9d3b53cc-3682-41ad-abea-9aff7659e334" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_function_1 <- function(x, y)\n", " {x = x + y + 2\n", " 2 * x}\n", "\n", "my_function_2 <- function(x, y)\n", " {x = x + y - my_function_1(x, y)\n", " 2 * x}\n", "\n", "my_function_2(2, 3)" ], "id": "f6d3f2b4-bb91-41e3-98da-0b58306bb138" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Luckily, we usually don’t have to define our own functions, since most\n", "useful built-in functions we need already come with R and its core\n", "packages. They do not require creation; they already exist for us,\n", "although they may require the importing of specific packages to be\n", "operable. We can always use the help `?` feature in R to learn more\n", "about a built-in function if we’re unsure. For example, `?max` gives us\n", "more information about the `max()` function.\n", "\n", "For more information about how you should read and use key functions,\n", "please refer to the [Function Cheat\n", "Sheet](https://cran.r-project.org/doc/contrib/Short-refcard.pdf).\n", "\n", "## 3.7 Errors\n", "\n", "Sometimes in our analysis we run into errors in our code; this happens\n", "to everyone and is not a reason to panic.\n", "\n", "Understanding the nature of the error we are confronted with is a\n", "helpful first step in finding a solution. There are two common types of\n", "errors:\n", "\n", "- **Syntax errors**: This is the most common error type. These errors\n", " result from invalid code statements/structures that R doesn’t\n", " understand. Suppose R speaks English. This error is representative\n", " of us asking it to help by speaking German, which would certainly\n", " not work! Here are some examples of common syntax errors: using a\n", " function for which an unloaded package is needed, misspelling of a\n", " command as R is case-sensitive, and unmatched parenthesis. How we\n", " handle syntax errors is case-by-case: we can usually solve syntax\n", " errors by reading the error message and searching it.\n", "\n", "- **Semantic errors**: These errors result from valid code that\n", " successfully executes but produces unintended outcomes. Again, let\n", " us suppose R speaks English. This error is representative of us\n", " asking R to hand us an apple in English, which R successfully\n", " understood, but it handed us a banana in return. This is not okay!\n", " How we handle semantic errors is also case-by-case: we can usually\n", " solve semantic errors by reading the associated error message and\n", " searching it for help/suggestions.\n", "\n", "## 3.8 Wrap Up\n", "\n", "In this notebook, we have learned the different ways data can be stored\n", "and structured in our R memory. We have also learned how to manipulate,\n", "extract and operate on data from different structures. Additionally, we\n", "have learned how to write a function to perform operations more\n", "efficiently. Now that we have all of this knowledge at our disposal, we\n", "can load in data and operate on it in the next module.\n", "\n", "## 3.9 Wrap-up Table\n", "\n", "| Command | Function |\n", "|----------------------------------|--------------------------------------|\n", "| `c()` | It creates a vector. |\n", "| `matrix()` | It creates a matrix. |\n", "| `list()` | It creates a list. |\n", "| `data.frame()` | It creates a data frame object. |\n", "| `typeof()` | It prints the type of the object in parenthesis. |\n", "| `str()` | It prints the structure of the object in parenthesis. |\n", "| `function(arg){code}` | It creates a function that takes *arg* as inputs and uses them to run the *code* detailed within curly brackets. |\n", "\n", "## References\n", "\n", "- [Useful R Cheat\n", " Sheets](https://www.rstudio.com/resources/cheatsheets/)" ], "id": "1ed25f13-7ebf-4fca-9076-ba56fbcabeb8" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "ir", "display_name": "R", "language": "r" } } }