# Don't delete this code! It should exist at the start of every Rmd file.
# It makes sure that your images knit correctly, and that your code chunks by default
# show up in the PDF, and don't get unhelpful warnings/messages.
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
Then, select the first two lines of code (with library()
and the read_csv()
functions. View()
is useful, but only to pull up your dataframe in a separate tab in RStudio). Hit cancel, then go back to your code chunk and paste the code. Now you have a variable that contains a dataframe for the data that you uploaded into RStudio :]
1 + 1
## [1] 2
echo = FALSE
, or message = FALSE
. These refer to the outcomes you want with your code chunk. When echo = FALSE
, we are telling R to not print out the code chunk when we knit the document. message = FALSE
tells R to not print out any long messages (such as when you load in packages). View other resources here about other things you can change! These are not required in this class, though.To change the size of a graph, include the arguments fig.width = _
and fig.height = _
. There isn’t a one-size-fits-all measurement here, so I would recommend playing around with these values until you like how your graph looks. Generally, I use fig.width = 10
, fig.height = 8
, if you want to start there!
Scripting is a helpful tool we talked about briefly in the first class! This type of coding will be useful if we ever want to just create code without interpreting it (as the only text you can include in scripts is code or comments). For instance, scripts do not handle code chunks, with separate areas for text—it’s all one big code chunk. That’s why hitting the run bottom runs everything in the script file, not just one part of the code!
The code at the very beginning of an RMarkdown document is called the YAML, and is noted by three dashes at the top (—) and at the bottom (—). Here is where you can specify info such as the title, what you want to document to output as when you knit it, and any formating pieces when you knit! For an example of what Alex wants the YAML to look like, go here on the website. If you’re interested in learning more, view this resource on YAMLs for PDFs, and this resource for HTML output :]
Before you can load in a package, make sure it’s installed! There are a few ways to do this: 1. Type in library(package_name)
. If the package is not loaded in R, a yellow bar will pop up at the top of your screen, and prompt you to install the package. Click “Install.” 2. In the console, write install.packages("your_package_name_here")
and click enter. 3. Run the install.packages("your_package_name_here")
in your RMarkdown. Just be sure to delete the code or comment it out before knitting!
Load packages once a session, at the top of your RMarkdown (after the setup chunk). This is because when you knit your document, it runs the code from top to bottom, and you want to make sure that all of the libraries are loaded before it runs code that may need those packages! To load a package, use the library()
function like so:
library(tidyverse)
To assign any information/data you have changed to be saved to a variable (essentially a shortcut you can use to grab that changed data), use the <-
key. For instance, if I want to rename my mariokart data to something shorter, I would write:
m <- mariokart
It is important to note that you can name your variables just about anything! Make sure it makes logical sense, though, and that if you needed to grab it later you would know what you’re referring to.
However: - Your variable cannot start with a number. For instance, 2_species is not a valid name, but two_species or species_2 would be. - Don’t write over another variable when naming things! It’s likely that you’ll want to refer back to old data at some point, and writing over it makes things complicated. - If you want to read more about naming conventions, feel free to read through this short document!
Using these brackets [] after a variable name allows you to pull out a certain value(s) from how you’ve stored it. If you have a vector, say nums <- c(1, 2, 3, 4, 5)
, you can grab the second value by saying nums[2]
. If you want to grab sequential values, you can write nums[2:4]
. This grabs the numbers in the vector from the second value to the fourth value (2, 3, and 4).
For a dataframe, which stores data in rows and columns, we can still use brackets, but now we have to consider not just one list of vectors, but instead asking for a specific column number and then row number.
data_set_name[1:15, 2]
.
mariokart[1:15, 2]
data_set_name[1:11,]
. To grab all of the rows but just column one, write: data_set_name[,1]
.mariokart[1:11,]
mariokart[,1]
In R, you can use the dollar sign in the same way that we would grab all of the rows for a specific column. Above we talk about how to grab all of the rows for columns two through four using brackets (writing data_set[2:4,]
). The dollar sign allows us to identify the name of a column we want to grab from the data. To call on all the rows in the third column in the mariokart
dataframe, Character
, we would write:
mariokart$Character
## [1] "Metal Mario" "Metal Mario" "Honey Queen"
## [4] "Metal Mario" "Honey Queen" "Honey Queen"
## [7] "Metal Mario" "Honey Queen" "Honey Queen"
## [10] "Yoshi" "Metal Mario" "Wario"
## [13] "Bowser" "Metal Mario" "Bowser"
## [16] "Metal Mario" "Metal Mario" "Wario"
## [19] "Wario" "Wario" "Metal Mario"
## [22] "Wario" "Metal Mario" "Metal Mario"
## [25] "Metal Mario" "Metal Mario" "Metal Mario"
## [28] "Bowser" "Metal Mario" "Metal Mario"
## [31] "Metal Mario" "Honey Queen" NA
## [34] "Heavy Mii" "Morton" "Morton"
## [37] "Heavy Mii CptFal" "Morton" "Dry Bowser"
## [40] "Morton" "Heavy Mii" "Morton"
## [43] "Morton" "Morton" "Heavy Mii"
## [46] "Morton" "Morton" "Morton"
## [49] "Morton" "Morton" NA
## [52] "Dry Bowser" "Bowser" "Morton"
## [55] "Morton" "Heavy Mii" "Morton"
## [58] "Morton" "Morton" "Dry Bowser"
## [61] "Wario" "Morton" "Heavy Mii Toad"
## [64] "Heavy Mii" "Morton" "Morton"
## [67] "Bowser" "Morton" "Morton"
## [70] "Dry Bowser" "Wario" "Morton"
## [73] "Wario" "Heavy Mii" "Heavy Mii Sonic"
## [76] "Dry Bowser" "Bowser" "Dry Bowser"
## [79] "Morton" "Morton" "Morton"
## [82] "Morton" "Morton" NA
## [85] "Wario" "Waluigi" "Baby Daisy"
## [88] "Waluigi" "Waluigi" "Waluigi"
## [91] "Lemmy" "Waluigi" "Waluigi"
## [94] "Rosalina" "Roy" "Roy"
## [97] "Waluigi" "Baby Daisy" "Roy"
## [100] "Baby Daisy" "Waluigi" "Waluigi"
## [103] "Roy" "Wario" "Baby Daisy"
## [106] "Baby Peach" "Waluigi" "Wario"
## [109] "Wario" "Baby Peach" "Lemmy"
## [112] "Lemmy" "Baby Peach" "Donkey Kong"
## [115] "Donkey Kong" "Dry Bowser" "Waluigi"
## [118] "Baby Daisy" "Baby Daisy" "Waluigi"
## [121] "Wario" "Donkey Kong" "Baby Peach"
## [124] "Waluigi" "Waluigi" "Baby Daisy"
## [127] "Waluigi" "Baby Daisy" "Baby Daisy"
## [130] "Waluigi" "Baby Daisy" "Wario"
## [133] NA "Wario" "Heavy Mii Mega Man"
## [136] "Wario" "Wario" "Heavy Mii"
## [139] "Wario" "Wario" "Waluigi"
## [142] "Waluigi" "Waluigi" "Roy"
## [145] "Wario" "Wario" "Rosalina"
## [148] "Dry Bowser" "Yoshi" "Wario"
## [151] "Waluigi" "Dry Bowser" "Heavy Mii Mario"
## [154] "King Boo" "Baby Daisy" "Waluigi"
## [157] "Donkey Kong" "Wario" "King Boo"
## [160] "Roy" "Waluigi" "Roy"
## [163] "Dry Bowser" "Donkey Kong" "Dry Bowser"
## [166] "Baby Peach" "Waluigi" "Baby Daisy"
## [169] "Heavy Mii Cpt Falcon" "Wario" "Roy"
## [172] "Wario" "Roy" "Baby Daisy"
## [175] "Baby Peach" "Waluigi" "Heavy Mii Crossing"
## [178] "Baby Daisy" "Roy" "Waluigi"
## [181] "Dry Bowser"
This prints out all of the characters ever raced with to achieve a world record time! (Gotta go fast).
However, what do we do when we want to call on multiple different columns, without using brackets? To do this, we turn to the c()
function.
This function will probably be the one you use the most, and love the most! It can combine together all sorts of different values, functions you’ve written, etc. all into one nice variable! - You may remember using this to combine multiple y values from your linear equation function in lab one. If not, never fear, I’ll show some examples!
# Adding together multiple columns
multiple_cols <- c(mariokart$Character, mariokart$Vehicle, mariokart$Tires)
multiple_cols
## [1] "Metal Mario" "Metal Mario" "Honey Queen"
## [4] "Metal Mario" "Honey Queen" "Honey Queen"
## [7] "Metal Mario" "Honey Queen" "Honey Queen"
## [10] "Yoshi" "Metal Mario" "Wario"
## [13] "Bowser" "Metal Mario" "Bowser"
## [16] "Metal Mario" "Metal Mario" "Wario"
## [19] "Wario" "Wario" "Metal Mario"
## [22] "Wario" "Metal Mario" "Metal Mario"
## [25] "Metal Mario" "Metal Mario" "Metal Mario"
## [28] "Bowser" "Metal Mario" "Metal Mario"
## [31] "Metal Mario" "Honey Queen" NA
## [34] "Heavy Mii" "Morton" "Morton"
## [37] "Heavy Mii CptFal" "Morton" "Dry Bowser"
## [40] "Morton" "Heavy Mii" "Morton"
## [43] "Morton" "Morton" "Heavy Mii"
## [46] "Morton" "Morton" "Morton"
## [49] "Morton" "Morton" NA
## [52] "Dry Bowser" "Bowser" "Morton"
## [55] "Morton" "Heavy Mii" "Morton"
## [58] "Morton" "Morton" "Dry Bowser"
## [61] "Wario" "Morton" "Heavy Mii Toad"
## [64] "Heavy Mii" "Morton" "Morton"
## [67] "Bowser" "Morton" "Morton"
## [70] "Dry Bowser" "Wario" "Morton"
## [73] "Wario" "Heavy Mii" "Heavy Mii Sonic"
## [76] "Dry Bowser" "Bowser" "Dry Bowser"
## [79] "Morton" "Morton" "Morton"
## [82] "Morton" "Morton" NA
## [85] "Wario" "Waluigi" "Baby Daisy"
## [88] "Waluigi" "Waluigi" "Waluigi"
## [91] "Lemmy" "Waluigi" "Waluigi"
## [94] "Rosalina" "Roy" "Roy"
## [97] "Waluigi" "Baby Daisy" "Roy"
## [100] "Baby Daisy" "Waluigi" "Waluigi"
## [103] "Roy" "Wario" "Baby Daisy"
## [106] "Baby Peach" "Waluigi" "Wario"
## [109] "Wario" "Baby Peach" "Lemmy"
## [112] "Lemmy" "Baby Peach" "Donkey Kong"
## [115] "Donkey Kong" "Dry Bowser" "Waluigi"
## [118] "Baby Daisy" "Baby Daisy" "Waluigi"
## [121] "Wario" "Donkey Kong" "Baby Peach"
## [124] "Waluigi" "Waluigi" "Baby Daisy"
## [127] "Waluigi" "Baby Daisy" "Baby Daisy"
## [130] "Waluigi" "Baby Daisy" "Wario"
## [133] NA "Wario" "Heavy Mii Mega Man"
## [136] "Wario" "Wario" "Heavy Mii"
## [139] "Wario" "Wario" "Waluigi"
## [142] "Waluigi" "Waluigi" "Roy"
## [145] "Wario" "Wario" "Rosalina"
## [148] "Dry Bowser" "Yoshi" "Wario"
## [151] "Waluigi" "Dry Bowser" "Heavy Mii Mario"
## [154] "King Boo" "Baby Daisy" "Waluigi"
## [157] "Donkey Kong" "Wario" "King Boo"
## [160] "Roy" "Waluigi" "Roy"
## [163] "Dry Bowser" "Donkey Kong" "Dry Bowser"
## [166] "Baby Peach" "Waluigi" "Baby Daisy"
## [169] "Heavy Mii Cpt Falcon" "Wario" "Roy"
## [172] "Wario" "Roy" "Baby Daisy"
## [175] "Baby Peach" "Waluigi" "Heavy Mii Crossing"
## [178] "Baby Daisy" "Roy" "Waluigi"
## [181] "Dry Bowser" "B Dasher" "Bruiser"
## [184] "B Dasher" "Bruiser" "Bruiser"
## [187] "B Dasher" "B Dasher" "Bruiser"
## [190] "B Dasher" "Egg 1" "Koopa Clown"
## [193] "B Dasher" "B Dasher" "B Dasher"
## [196] "B Dasher" "B Dasher" "B Dasher"
## [199] "B Dasher" "B Dasher" "B Dasher"
## [202] "B Dasher" "B Dasher" "B Dasher"
## [205] "B Dasher" "Zucchini" "Blue 7"
## [208] "B Dasher" "B Dasher" "Blue 7"
## [211] "B Dasher" "B Dasher" "B Dasher"
## [214] NA "Gold Standard" "Streetle"
## [217] "Streetle" "Streetle" "Streetle"
## [220] "Mach 8" "Streetle" "Streetle"
## [223] "Blue Falcon" "Blue Falcon" "Streetle"
## [226] "Blue Falcon" "Pipe Frame" "Streetle"
## [229] "Streetle" "Blue Falcon" "Blue Falcon"
## [232] NA "Streetle" "Streetle"
## [235] "Blue Falcon" "Blue Falcon" "Blue Falcon"
## [238] "Streetle" "Blue Falcon" "Streetle"
## [241] "Streetle" "Tanooki Kart" "Mach 8"
## [244] "Streetle" "Mach 8" "Blue Falcon"
## [247] "Streetle" "Pipe Frame" "Mach 8"
## [250] "Blue Falcon" "Blue Falcon" "Gold Standard"
## [253] "Pipe Frame" "Pipe Frame" "W 25 Silver Arrow"
## [256] "Blue Falcon" "Gold Standard" "Streetle"
## [259] "Mach 8" "Streetle" "Mach 8"
## [262] "Streetle" "Streetle" "Streetle"
## [265] NA "Wild Wiggler" "Mr. Scooty"
## [268] "Biddybuggy" "Mr. Scooty" "Biddybuggy"
## [271] "Mr. Scooty" "Biddybuggy" "Mr. Scooty"
## [274] "Biddybuggy" "Streetle" "Biddybuggy"
## [277] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [280] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [283] "Biddybuggy" "Streetle" "Sport Bike"
## [286] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [289] "Sports Coupe" "Inkstriker" "Biddybuggy"
## [292] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [295] "Biddybuggy" "Biddybuggy" "Mach 8"
## [298] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [301] "Mr. Scooty" "Splat Buggy" "Biddybuggy"
## [304] "Biddybuggy" "Biddybuggy" "Mr. Scooty"
## [307] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [310] "Biddybuggy" "Biddybuggy" "Biddybuggy"
## [313] "Mach 8" NA "Wild Wiggler"
## [316] "Biddybuggy" "Biddybuggy" "W 25 Silver Arrow"
## [319] "W 25 Silver Arrow" "Blue Falcon" "Yoshi Bike"
## [322] "Wild Wiggler" "Wild Wiggler" "Streetle"
## [325] "Mach 8" "Wild Wiggler" "Teddy Buggy"
## [328] "Biddybuggy" "Wild Wiggler" "Biddybuggy"
## [331] "Sneeker" "Wild Wiggler" "Landship"
## [334] "Sport Bike" "Landship" "Biddybuggy"
## [337] "Teddy Buggy" "Biddybuggy" "Biddybuggy"
## [340] "Streetle" "W 25 Silver Arrow" "Wild Wiggler"
## [343] "Biddybuggy" "W 25 Silver Arrow" "Biddybuggy"
## [346] "Mach 8" "Biddybuggy" "Biddybuggy"
## [349] "Biddybuggy" "Mach 8" "Mach 8"
## [352] "Biddybuggy" "Wild Wiggler" "Mr. Scooty"
## [355] "Mr. Scooty" "Mr. Scooty" "Biddybuggy"
## [358] "Teddy Buggy" "Biddybuggy" "Biddybuggy"
## [361] "Biddybuggy" "Mach 8" "Red Monster"
## [364] "Slick" "Slick" "Red Monster"
## [367] "Slick" "Red Monster" "Slick"
## [370] "Slick" "Slick" "Wood"
## [373] "Red Monster" "Slick" "Slick"
## [376] "Red Monster" "Slick" "Slick"
## [379] "Red Monster" "Slick" "Slick"
## [382] "Red Monster" "Slick" "Mushroom"
## [385] "Slick" "Slick" "Red Monster"
## [388] "Red Monster" "Slick" "Slick"
## [391] "Red Monster" "Slick" "Mushroom"
## [394] "Slick" NA "Slick"
## [397] "Leaf Tires" "Slick" "Cyber Slick"
## [400] "Slick" "Slick" "Cyber Slick"
## [403] "Slick" "Cyber Slick" "Triforce Tires"
## [406] "Slick" "Slick" "Slick"
## [409] "Slick" "Cyber Slick" "Cyber Slick"
## [412] "Slick" NA "Cyber Slick"
## [415] "Slick" "Slick" "Cyber Slick"
## [418] "Cyber Slick" "Slick" "Cyber Slick"
## [421] "Slick" "Cyber Slick" "Leaf Tires"
## [424] "Slick" "Cyber Slick" "Slick"
## [427] "Cyber Slick" "Cyber Slick" "Slim"
## [430] "Slick" "Slick" "Slick"
## [433] "Slick" "Slim" "Slim"
## [436] "Slick" "Slick" "Slick"
## [439] "Standard" "Slick" "Slick"
## [442] "Slick" "Slick" "Cyber Slick"
## [445] "Cyber Slick" NA "Roller"
## [448] "Azure Roller" "Azure Roller" "Azure Roller"
## [451] "Leaf Tires" "Azure Roller" "Azure Roller"
## [454] "Azure Roller" "Azure Roller" "Azure Roller"
## [457] "Azure Roller" "Azure Roller" "Azure Roller"
## [460] "Azure Roller" "Azure Roller" "Azure Roller"
## [463] "Roller" "Leaf Tires" "Azure Roller"
## [466] "Slim" "Azure Roller" "Azure Roller"
## [469] "Roller" "Azure Roller" "Azure Roller"
## [472] "Azure Roller" "Azure Roller" "Azure Roller"
## [475] "Azure Roller" "Azure Roller" "Azure Roller"
## [478] "Leaf Tires" "Azure Roller" "Azure Roller"
## [481] "Azure Roller" "Leaf Tires" "Slim"
## [484] "Azure Roller" "Azure Roller" "Azure Roller"
## [487] "Leaf Tires" "Roller" "Leaf Tires"
## [490] "Azure Roller" "Azure Roller" "Azure Roller"
## [493] "Azure Roller" "Leaf Tires" NA
## [496] "Azure Roller" "Azure Roller" "Button"
## [499] "Azure Roller" "Leaf Tires" "Crimson Slim"
## [502] "Button" "Leaf Tires" "Azure Roller"
## [505] "Azure Roller" "Leaf Tires" "Azure Roller"
## [508] "Azure Roller" "Azure Roller" "Leaf Tires"
## [511] "Roller" "Crimson Slim" "Leaf Tires"
## [514] "Azure Roller" "Metal" "Azure Roller"
## [517] "Azure Roller" "Roller" "Azure Roller"
## [520] "Azure Roller" "Azure Roller" "Azure Roller"
## [523] "Azure Roller" "Azure Roller" "Leaf Tires"
## [526] "Azure Roller" "Leaf Tires" "Azure Roller"
## [529] "Azure Roller" "Leaf Tires" "Wood"
## [532] "Crimson Slim" "Azure Roller" "Azure Roller"
## [535] "Roller" "Button" "Azure Roller"
## [538] "Leaf Tires" "Azure Roller" "Azure Roller"
## [541] "Azure Roller" "Azure Roller" "Leaf Tires"
# Adding together the results of multiple functions:
FUNctions <- c(seq(0,10, length.out = 4), rep("boo", 8), rep("halloween", 13))
FUNctions
## [1] "0" "3.33333333333333" "6.66666666666667" "10"
## [5] "boo" "boo" "boo" "boo"
## [9] "boo" "boo" "boo" "boo"
## [13] "halloween" "halloween" "halloween" "halloween"
## [17] "halloween" "halloween" "halloween" "halloween"
## [21] "halloween" "halloween" "halloween" "halloween"
## [25] "halloween"
library()
command to grab the dplyr package from where it usually lives in the background of R to the forefront!# Taking my mariokart data, and renaming it to mariokart_measures. AND THEN
mariokart_measures <- mariokart %>%
# Creating a new column called hello AND THEN
mutate(hello = "hello")
print(mariokart_measures)
## # A tibble: 181 × 8
## Date Duration Character Vehicle Tires Glider Game hello
## <date> <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 2020-12-28 115 Metal Mario B Dasher Red Monster Flower Gl… Mariok… hello
## 2 2021-02-02 79 Metal Mario Bruiser Slick Swooper Mariok… hello
## 3 2020-12-15 128 Honey Queen B Dasher Slick Beast Gli… Mariok… hello
## 4 2021-02-02 79 Metal Mario Bruiser Red Monster Super Gli… Mariok… hello
## 5 2020-12-12 131 Honey Queen Bruiser Slick Peach Par… Mariok… hello
## 6 2020-09-14 220 Honey Queen B Dasher Red Monster Peach Par… Mariok… hello
## 7 2020-01-12 466 Metal Mario B Dasher Slick Gold Glid… Mariok… hello
## 8 2019-12-28 481 Honey Queen Bruiser Slick Beast Gli… Mariok… hello
## 9 2021-02-10 71 Honey Queen B Dasher Slick Beast Gli… Mariok… hello
## 10 2019-03-27 757 Yoshi Egg 1 Wood Super Gli… Mariok… hello
## # … with 171 more rows
mariokart %>%
filter(Character == "Metal Mario")
mariokart %>%
filter(Duration == 466)
mariokart %>%
filter(Duration != 154)
mariokart %>%
filter(Character != "Metal Mario")
mariokart %>%
filter(Duration > 400)
mariokart %>%
filter(Duration <= 100)
column_name ==
for each occurrence, you can use the %in% to refer to a vector with all your values of interest. See below for an example!mariokart %>%
filter(Character == "Yoshi" | Character == "Heavy Mii")
mariokart %>%
filter(Character == "Yoshi" & Vehicle == "Egg 1")
mariokart %>%
filter(Character %in% c("Yoshi", "Wario", "Morton"))
From the website:
Visit this link to look at a cheat sheet for dplyr made by the creators of the package! It has some pretty neat visualizations, if you’re more of a visual learner :)
To print out a dataframe (or any vector/object you’re working with in R), you can just write its name.
mariokart
A shortcut I use to view objects as I’m coding it to select just the variable name (whether or not it is on a line with other code or not), and hit Command + Return if a Mac and Control + Enter if a Windows computer. This will run just the code that is selected, allowing you to view the object.
I’m going to give an example of what the code looks like for a ggplot
graph!
ggplot(data = your_data_here, aes(x = your_x_here,
y = your_y_here_if_applicable,
color = your_color_here_if_applicable,
etc...)) +
geom_your_geom_here()
If you fill in all of the spots with your data and geoms/aesthetics of interest, you will get a ggplot()
™!
Aesthetics refer to all parts of your graph that you can use to show a variable! For instance, your x and y axes are aesthetics for the variables you display on them, as is things like color and size. See the below graph for an example that uses all of these aesthetics!
Geometries refer to the type of graph you want to create, or the geometry that the graph will take on. This will include points (scatterplots), bars (for histograms or bar plots), and so on and so forth. To see a comprehensive list of all the aesthetics and geometries you can manipulate in a ggplot
, visit their cheat sheet here!
To make a function in R, you’re essentially setting side a bit of code, so you don’t have to run the same code over and over again. Thus, you give the function an input and using the written code it will transform it into some sort of an output. See below for an example!
your_function_name <- function(input1, input2, ...) {
# Put all of the body of your code in these brackets!
y <- m * x + b
# To print your output, run the variable name
y
# Additionally, if you're just working with a couple of lines and do not
# need to save your data, you could just write
m * x + b
# for it to print your output.
}
Find correlations by running the cor()
function and feeding in your dataframe! Additionally, you can feed in a data frame with all numerical values to the pairs()
function to create a bunch of scatterplots. Also, the GGally library (made by Reed alum Grayson White), you can create scatterplots in ggplot!
library(GGally)
mariokart_numeric <- mariokart %>%
select_if(is.numeric)
ggpairs(mariokart_numeric)
(The Mariokart data isn’t the best example here, as there is only one numeric variable, but for other datasets such as iris and county that have lots of numeric data, this will be super useful!)
Below, I will walk through the syntax of a linear model!
# The lm stands for "linear model"
name_of_your_model <- lm(your_response_variable ~ your_explanatory_variable,
data = your_data_here)
# To view info on your model, use the summary command
summary(name_of_your_model)
This model will store the information required to make a line fit your data as best as possible, with the least distance from all of the points!
Another way to make a linear model (though without all the details you’d get above) is to make a scatterplot and fit a geom_smooth
line to it. See below!
ggplot(data = your_data, aes(x = your_x,
y = your_y)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
# This same method will also create three separate linear models if you've added in
# another aesthetics for a new variable!