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
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
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.
You can type anything within the document, but if you want to run code you must create a code block.
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:
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!
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.
{width="369" height="190"}
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.
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
# 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
toy <- (8+6)^7
(toy + 8 + 100) / 6[1] 17568935
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.
# 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
toy - toy + toy[1] 105413504
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.
use a new code chunk for every line of code until you get to graphing.These are qualities that you can utilize, but are not required to get through the class.
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
The first is title: "Untitled"
The second and third is format: html & editor: visual. Ignore both of these.
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).
If you change the words after the colon, to something like this:
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.
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:
Headings are accessible in two different ways. The easiest way to make a heading is through the same tab line as the “Insert” button.
this is normal text
If you got confused anywhere, no worries! Just focus on the most important parts of a quarto, which are
words incode inYou 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.
The Documents / Scripts
The Console
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:
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.
The Global Environment
one <- 1
five <- 8
group <- c(5, 8, 4, 4)
one <- 11The last square
How to save
Ctrl + S where you hold Ctrl button and quickly press the “S” key. This will do 1 of two things
Functions
# 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
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.
The tabs in R
The last aspect of R you should know are the tabs at the top.
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).
Import the dataset
Try to import a data set. Remember a couple of things for guidance:
Where do you go first to import data?
Where do you find the code that shows you did it properly?
How do you save the code to a Quarto doc?
What should the code look like?
look here for the answer:
protestant_work_ethic_cleaned <- read.csv("~/Desktop/102/tutor/datasets/protestant_work_ethic_cleaned.csv", stringsAsFactors=TRUE)Rename the dataset
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:
You want a short name for the data set, and one that you know what it means
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?
Look here for the answer:
prot <- read.csv("~/Desktop/102/tutor/datasets/protestant_work_ethic_cleaned.csv", stringsAsFactors=TRUE)Check to make sure the data loaded correctly.
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:
There is a line of code that creates the dataset pop up.
There is a line of code that shows you the first 6 rows of the data.
Look here for the answer:
# 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