Setting up A Document

Starting an Assignment

First, before we can handle any coding, we want to create a space for us to code and write text in. This is called a Quarto Document. To create one of these documents, you click on the paper tab that has a green plus sign on it at the top left corner of R. The drop down should provide many options, but the only part you want to focus on is the “New File” and click on the tab that says “Quarto”. When you create a new Quarto, it should look like the image below:

There are a couple aspects to a Quarto document you can know, and should know. I list them below

Most Important

The most important aspects of a quarto document will be listed here. These are the crucial areas that you must know to let quarto work smoothly.

  1. You can type anything within the document, but if you want to run code you must create a code block.

    1. A code block can be created using the short cut Ctrl + Alt + i. Press and hold both Control and Alt buttons on the keyboard, then quickly tap the i key. A code block should pop up and look like this:

    2. You can also make a code chunk by going to the top of the quarto document (not above the R tabs!) and clicking “Insert”. Then “New code chunk” then “R”. This is inserting a code chunk with the language R. This method is the exact same as Ctrl + Alt + i, just a little longer of a process. Try both ways in your own document!

  2. Code Chunks (aka, a code block)

    Code chunks are great, but they can be a pain sometimes. Its like working with a horse, it can be predictable as long as you are confident. If you’re not confident, we will get you there, do not worry.

    1. First, what you need to know about code chunks is that a code block is where you only run code. Do not put text in here, ensure any and all text is outside of the code block. If there is text, the file wont render (load into a pdf). That will halt the entire loading process, and we want to avoid that! This is what that looks like:
    ![](images/clipboard-390217186.png){width="369" height="190"}
    1. Second, when you successfully run something in R, there will be a little green bar that very, very quickly pops up. When you run a code, check to see if that occurs. If so, then you should be good, but check the other two spaces to ensure it did run (console and global environment). If not, there is an issue that you must address. There may be a pop up that says “Warning:” or “Error:”. Read both messages, as it will describe what went wrong. It may be helpful or you may not understand the message at all, but in either case, go back to your code and try to understand what you wrote and why you wrote it. Then, figure out what may have gone wrong. There may also be a little circle with an x through it, that means R is catching an error before you run the code. Check why it is like that, because if you try to run that code with a little x near it, it wont run, so it is best to address the problem before R yells at you more.

    2. Third, R reads a single code chunk like a book. R will see a code chunk and group every line of code together and run each line as many times as you tell it to. This is good, and bad. For example

    Code
    # Here is a code chunk, R will run each line of code below everytime I click the green arrow within the code chunk. 
    sum(c(5,4,8,23,3,3,3,6))
    [1] 55
    Code
    toy <- (8+6)^7
    (toy + 8 + 100) / 6
    [1] 17568935
    Code
    toy - toy
    [1] 0

    The output is everything I listed within this code chunk. This is often needed for some code, like plotting, but sometimes running everything over and over can create heavy strain on the computer. And, the worst case, you may override a code you had. R is a program that will replace a variable if it is named the same thing.

    Code
    # We had a variable called toy above (understand where toy is from and how I made that variable)
    # Now, if I run this entire code chunk, something inconvenient is going to happen. See if you can spot where.
    toy <- (8+6)^7
    toy + toy + toy
    [1] 316240512
    Code
    toy - toy + toy
    [1] 105413504
    Code
    toy <- 5
    toy + toy + toy
    [1] 15

    Toy + toy + toy was originally some big number, but the next time I did Toy + toy + toy, it equaled 15. This is because I used the same name for a variable as in the past, so when I ran this code, whatever toy was saved as in the global environment last, that is what R used.

    • That may have been confusing. The main point you need to take away from this is that there are times when to use the same code chunk and when to use a new one. I would recommend using a new one all the time, unless you are graphing. But you are not graphing in this tutorial, so as a rule of thumb: use a new code chunk for every line of code until you get to graphing.

Frivolous Attributes

These are qualities that you can utilize, but are not required to get through the class.

  1. There are a bunch of texts at the top of the quarto document when it loads. These help make the pdf look pretty. This is what they look like

    1. The first is title: "Untitled"

      1. This code provides an overall title for the pdf.
    2. The second and third is format: html & editor: visual. Ignore both of these.

    3. The fourth code you can input in this space is author: First Last. This code will provide an output that attributes the pdf to your name (or whatever you put as the author).

    4. If you change the words after the colon, to something like this:

    5. it will output like this:

      Notice how the title at the top of the document changed to what I wrote after the colon? There was also an added “Author” part under the title. Then it jumps into the words that I wrote within the document. Cool right?! Play around with it, but ensure the text on the left is red : text on the right.

  2. There are Headings that you can put within the document that allow for easy navigation. These headings, depending on the number you choose, will provide different levels of tab importance, which you can see to the right of the quarto tab. These are tabs that you can click on to get to that section within the document. This is what they look like:

    1. Headings are accessible in two different ways. The easiest way to make a heading is through the same tab line as the “Insert” button.

      1. Instead of inserting. however, you look to the left and find “Normal”. That is the default text that you are reading currently. If you click on it, though, there will be a drop down that will list 1-6 types of headers. I won’t go into exactly what they do, so I recommend playing around with each one and see how it changes with the output.

This is heading 1

This is heading 2

This is heading 3

This is heading 4

this is heading 5
this is heading 6

this is normal text

For Practice

If you got confused anywhere, no worries! Just focus on the most important parts of a quarto, which are

  1. The area you write words in
  2. The area you wrote code in

You should also familiarize yourself with R and how it works. There are many resources available to you that you can use to get more comfortable with this interface, which I would recommend look at. I provided the pdf to you in an email, but there are youtube videos, articles, and the official R website that will help guide you through R. I will list some aspects of R that are good to know, but it this will a non-exhaustive description.

  1. The Documents / Scripts

    1. documents or scripts are where you work in. Quarto is a type of script. Work in quarto or R script. This is what a Quarto and R Script look like:

  2. The Console

    1. The Console is the output for every code you run. Consider it a type of history. You can look back there and see every code you have run within the time you have kept R open. If you close R, that console will erase. This is what the console looks like, both full of history and empty:

    2. The console is another space you can make sure your code ran. Again, this is the space for code history, so if the code ran correctly, it will pop up down here. Check here to make sure the code ran, and ran correctly.

  3. The Global Environment

    1. This is probably one of the most important parts of R. The Global Environment is where all of your variables are stored, and they wont go away unless you tell them to. If you save a variable with a name, it will pop up here. Variables can include data sets, numbers, lists, and much more.
    2. To test it out, save a bunch of variables with unique names and see them pop up within the global environment. See what names work and don’t. [Hint: use full/partial words/letters with no spaces or special characters] Notice that if you name a variable the same name with different outputs, whatever the most recent run of code will be what the variable is named. An example is below:
    Code
    one <- 1
    five <- 8
    group <- c(5, 8, 4, 4)
    one <- 11

  4. The last square

    1. Im not sure what this space is called, but this is how you access files and where graphs pop up. This is not important right now, so you can leave this square alone

  5. How to save

    1. To save the document, you can do one of three ways. The first option is using the shortcut version which is Ctrl + S where you hold Ctrl button and quickly press the “S” key. This will do 1 of two things
      1. If you have saved the document to your computer before, that will simply save the document and keep everything you have worked on.
      2. If you have NOT saved the document before (save as), then a popup window will occur that will force you to save the document with a name. This is when you should save the document to your computer with a recognizable name and in a file you know and will remember.
    2. The second way is manual, where you go to the top of your screen and click “save” or “save as”.
    3. The third way is clicking the little floppy disk icon either above the “Insert” and “Heading” line, or the one at the very top of the R user interface. Find where they are in the image below, then try to find them on your computer.

  6. Functions

    1. Functions is a difficult topic, but this is essentially every single code line you run.
    Code
    # The arrow is a function that assigns a name to whatever operation you are doing. Anything that the arrow is pointing at is the name of the variable
    this_is_the_name_of_this_variable <- sum(c(6,6,6,6,6))
    
    # Most functions use parentheses to indicate it is a function. You have used many functions areadly, such as read.csv(), head(), view(), sum(), and more
    prot <- read.csv("~/Desktop/102/tutor/datasets/protestant_work_ethic_cleaned.csv", stringsAsFactors=TRUE)
    # View(prot)
    head(prot)
      Q1A Q2A Q3A Q4A Q5A Q6A Q7A Q8A Q9A Q10A Q11A Q12A Q13A Q14A Q15A Q16A Q17A
    1   4   1   5   5   5   2   4   3   5    5    3    4    2    3    5    2    5
    2   4   4   4   5   4   2   5   2   2    2    4    4    4    2    2    4    4
    3   3   3   4   2   2   3   3   2   3    3    4    3    3    4    4    3    3
    4   4   2   4   4   1   4   5   5   5    5    4    4    2    5    5    5    5
    5   4   1   5   1   1   1   5   5   5    2    1    2    5    5    5    4    2
    6   1   1   2   1   5   1   5   1   5    3    2    4    4    4    5    4    3
      Q18A Q19A country introelapse testelapse surveyelapse TIPI1 TIPI2 TIPI3 TIPI4
    1    1    2      US           5        117           68     2     3     3     3
    2    4    5      NZ          21        200          272     2     5     7     3
    3    2    2      GR          14        118          105     1     5     3     6
    4    4    2      SN         762        144          136     7     1     5     2
    5    4    2      US        1118        103          111     2     6     1     7
    6    1    1      US          71        152          381     2     2     1     4
      TIPI5 TIPI6 TIPI7 TIPI8 TIPI9 TIPI10 VCL1 VCL2 VCL3 VCL4 VCL5 VCL6 VCL7 VCL8
    1     4     6     4     4     4      3    1    1   NA    1    1   NA   NA    1
    2     6     6     5     1     6      1    1    1    1    1    1   NA   NA   NA
    3     5     6     3     5     3      5    1    1    1    1   NA   NA   NA    1
    4     7     3     7     5     6      1    1    1    1    1    1   NA   NA    1
    5     7     7     7     7     1      1    1    1   NA    1    1   NA   NA   NA
    6     7     3     6     7     3      1    1    1    1    1    1   NA    1    1
      VCL9 VCL10 VCL11 VCL12 VCL13 VCL14 VCL15 VCL16 education urban gender engnat
    1   NA     1    NA    NA     1     1     1     1         3     2      1      1
    2   NA     1    NA    NA     1     1     1     1         2     3      2      1
    3   NA     1    NA     1    NA     1     1     1         2     2      2      2
    4   NA     1    NA    NA     1     1     1     1         2     2      2      1
    5   NA     1    NA    NA     1     1     1     1         2     1      2      1
    6   NA     1     1    NA     1     1     1     1         4     3      2      1
      age screenw screenh hand religion orientation race voted married familysize
    1  24    1920    1080    1        6           1   11     2       1          2
    2  66     360     640    2        6           1   16     2       3          5
    3  17    1280    1024    1        2           1   16     2       1          4
    4  23    1920    1080    2        2           1   16     2       1          3
    5  19    1093     615    1        1           1   16     2       1          2
    6  40     320     568    1        7           2   16     2       3          3
                 major
    1 Computer Science
    2                 
    3                 
    4        dietetics
    5                 
    6      Philosophy 
    Code
    sum(4+4)
    [1] 8

    If you every know a function, but are not sure what arguments it needs, either look it up or use ?function in an r code chunk to see what advice R can give you. Try it yourself, but an exmaple is below.

  7. The tabs in R

    1. The last aspect of R you should know are the tabs at the top.

    2. Here, you can see I have many many tabs open, such as “notes for 101”, “prot”, “Untitled2”, etc. These are all separate documents that I have open. If I ever want to access one of these documents, all I have to do is click on the tab name and it will pop up on the screen. If I want to close one out, I will press the little “x” next to the name. If I am worried if I saved the document, check to see if the tab is a different color than white/black. If it is red or blue, then you have not saved the document and you should soon. Get into the habit of always saving your document, just in case something evil in the computer happens and it closes R and all your hard work is lost. We don’t want that, so ensure your tabs are the normal color (white/black).


Lab 4 Practice Review

Problem 1

  1. Import the dataset

    1. Try to import a data set. Remember a couple of things for guidance:

      1. Where do you go first to import data?

      2. Where do you find the code that shows you did it properly?

      3. How do you save the code to a Quarto doc?

      4. What should the code look like?

    2. look here for the answer:

    Code
    protestant_work_ethic_cleaned <- read.csv("~/Desktop/102/tutor/datasets/protestant_work_ethic_cleaned.csv", stringsAsFactors=TRUE)
  2. Rename the dataset

    1. Now that you imported the data, R will give it the same name you downloaded it as. Lets rename it to something shorter and more recognizable. Reminder:

      1. You want a short name for the data set, and one that you know what it means

      2. Ensure the code ran correctly. Did you see the green bar? Did you see it pop up in the console? Is the name you chose in the global environment?

    2. Look here for the answer:

    Code
    prot <- read.csv("~/Desktop/102/tutor/datasets/protestant_work_ethic_cleaned.csv", stringsAsFactors=TRUE)
  3. Check to make sure the data loaded correctly.

    1. Now that you gave the dataset a unique name, we want to make sure you loaded it correctly. There are two ways to do it, and as a Reminder:

      1. There is a line of code that creates the dataset pop up.

      2. There is a line of code that shows you the first 6 rows of the data.

    2. Look here for the answer:

    Code
    # View(prot)
    head(prot)
      Q1A Q2A Q3A Q4A Q5A Q6A Q7A Q8A Q9A Q10A Q11A Q12A Q13A Q14A Q15A Q16A Q17A
    1   4   1   5   5   5   2   4   3   5    5    3    4    2    3    5    2    5
    2   4   4   4   5   4   2   5   2   2    2    4    4    4    2    2    4    4
    3   3   3   4   2   2   3   3   2   3    3    4    3    3    4    4    3    3
    4   4   2   4   4   1   4   5   5   5    5    4    4    2    5    5    5    5
    5   4   1   5   1   1   1   5   5   5    2    1    2    5    5    5    4    2
    6   1   1   2   1   5   1   5   1   5    3    2    4    4    4    5    4    3
      Q18A Q19A country introelapse testelapse surveyelapse TIPI1 TIPI2 TIPI3 TIPI4
    1    1    2      US           5        117           68     2     3     3     3
    2    4    5      NZ          21        200          272     2     5     7     3
    3    2    2      GR          14        118          105     1     5     3     6
    4    4    2      SN         762        144          136     7     1     5     2
    5    4    2      US        1118        103          111     2     6     1     7
    6    1    1      US          71        152          381     2     2     1     4
      TIPI5 TIPI6 TIPI7 TIPI8 TIPI9 TIPI10 VCL1 VCL2 VCL3 VCL4 VCL5 VCL6 VCL7 VCL8
    1     4     6     4     4     4      3    1    1   NA    1    1   NA   NA    1
    2     6     6     5     1     6      1    1    1    1    1    1   NA   NA   NA
    3     5     6     3     5     3      5    1    1    1    1   NA   NA   NA    1
    4     7     3     7     5     6      1    1    1    1    1    1   NA   NA    1
    5     7     7     7     7     1      1    1    1   NA    1    1   NA   NA   NA
    6     7     3     6     7     3      1    1    1    1    1    1   NA    1    1
      VCL9 VCL10 VCL11 VCL12 VCL13 VCL14 VCL15 VCL16 education urban gender engnat
    1   NA     1    NA    NA     1     1     1     1         3     2      1      1
    2   NA     1    NA    NA     1     1     1     1         2     3      2      1
    3   NA     1    NA     1    NA     1     1     1         2     2      2      2
    4   NA     1    NA    NA     1     1     1     1         2     2      2      1
    5   NA     1    NA    NA     1     1     1     1         2     1      2      1
    6   NA     1     1    NA     1     1     1     1         4     3      2      1
      age screenw screenh hand religion orientation race voted married familysize
    1  24    1920    1080    1        6           1   11     2       1          2
    2  66     360     640    2        6           1   16     2       3          5
    3  17    1280    1024    1        2           1   16     2       1          4
    4  23    1920    1080    2        2           1   16     2       1          3
    5  19    1093     615    1        1           1   16     2       1          2
    6  40     320     568    1        7           2   16     2       3          3
                 major
    1 Computer Science
    2                 
    3                 
    4        dietetics
    5                 
    6      Philosophy