library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.4 ✔ tibble 3.3.1
## ✔ purrr 1.2.0 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#1A: Reproduce the following plots with the specified datasets (mtcars)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#ggplot = function used to make graph
#mtcars = dataset
#aes = aesthetic mappings or lets use describe variables such as X- and Y-axes (x = weight or wt, y = miles per gallon or mpg)
#geom_point = used for scatterplots; each observation is one of the black data points on graph
#stat_smooth = use to create a line that is a smoothed mean of Y that is condition of X, helps higlight trends in the data
#1B: Reproduce the following plots with the specified datasets (mtcars)
ggplot(mtcars, aes(x=wt, y=mpg, colour = as.factor(cyl))) + geom_point()
#ggplot = function used to make graph
#mtcars = dataset
#aes = aesthetic mappings or lets use describe variables such as X- and Y-axes (x = weight or wt, y = miles per gallon or mpg)
#colour = as.factor(cyl) -> this helps turn a column with numeric entries (cyl or # of cylinders) into a column with factor entires; each factor getting its own color. On graph, we can map these entries to a given datapoint.
#geom_point = used for scatterplots; each observation is one of the black data points on graph
#1C: Reproduce the following plots with the specified datasets (airquality)
ggplot(airquality, mapping = aes(x=Month)) + geom_bar() + coord_flip()
##ggplot = function used to make graph
#airquality= dataset
#aes = aesthetic mappings; lets use describe elements of the graph like the x-axis, which is for the month
#geom_bar() = creates a bar graph where each bar represents the number of cases for the variable in (in this case, number of times each month is represented in the set)
#coord_flip = flips the original bar graph where "month" goes from x-axis to y-axis and 'count' goes from y-axis to x-axis
#1D: Reproduce the following plots with the specified datasets (airquality)
ggplot(airquality, aes(x=Wind, y=Temp)) + geom_point() + stat_smooth()+ facet_wrap(.~Month, nrow=2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
##ggplot = function used to make graph
#airquality= dataset
#aes = aesthetic mappings; lets use describe elements of the graph like the x-axis (wind) and the y-axis (temperature)
#geom_point() = used for scatterplots; each observation is one of the black data points on graph
#stat_smooth = use to create a line that is a smoothed mean of Y that is condition of X, helps higlight trends in the data
#facet_wrap (.~Month, nrow=2) this allows us to plot subsets based on certain variables (in this case 'months'); nrow allows us to split the total number of subsetted plots into 2 rows
#2A: Create a function called comp_area(radius) to calculate the area (A) of a circle for a given radius (r) using the formula 𝐴 = 𝜋 × r^2. Test the function by finding the area of a circle with a radius of 5.5 cm.
comp_area <- function(radius)
{
area<-pi*radius*radius
return(area)
}
comp_area(5.5)
## [1] 95.03318
#comp_area = name of function
#argument or function(radius)= what we are going to imput into the function; in this case it is 'radius'
#actions or area<- pi*radius*radius = in this case, we want to compute the area of a circle, which is computed by the pi * radius-squared
#return(area) or output = this produces the area of the circle based on the formula above
#comp_area(5.5) = this computes the area of a circle when its radius is 5.5 cm, which is 95.033 cm
#2B: Create a function, sq_dif(x, y), that does the following: a) take two arguments x and y with default values of 2 and 3; b) take the difference of x and y values; c) square the difference; d) return the final value.
sq_dif<- function(x=2, y=3) {
return((x-y)^2)
}
#sq_dif = name of function
#(x=2, y=3) = the argument of the function, in this case we want to set the default x=2 and y=3
#return(x-y)^2) = this is the action of the formula, which takes the difference of X & Y cariables and then squares the different -> which then produces that final value
print(sq_dif()) #this computes the function with default values (X =2, y=3), final value is 1
## [1] 1
sq_dif(10,5) #this computes the function is X =10 & Y =5, final value is 25
## [1] 25
#2C -1 : Create a function is_odd(x) that returns TRUE when x is even and FALSE otherwise.
is_odd <- function(x) {
x %% 2 == 0
}
#is_odd = name of function
#function(x) = argument of function --> tells us that we will input a number in later on
# x %%2 == 0 -> this tells us that whether a number (x) - when divided by 2 - has a remainder of 0 or not. An EVEN number would have a remainder of 0 after being divided by 2 (TRUE). An ODD number would *not* have a remainder of 0 after being divided by 2 (FALSE)
is_odd(6) #example - 6 is an even number so the answer is TRUE
## [1] TRUE
is_odd(5) #example - 5 is an odd number to the answer is FALSE
## [1] FALSE
#2C - 2: Then, using is_odd(x), write a function odd_in(vec) that returns a vector comprised of the odd integers in a vector vec of integers.
odd_in<- function (vector) {
logical_vector <- is_odd(vector)
return(vector[!logical_vector])
}
#odd_in = name of function
#function (vector) - argument of functions --> tells us that we will only input vectors with this function
#logical_vector <- is_odd(vector) = this applies the function 'is_odd' to any vector I input into this new function
#return(vector[!logical_vector]) = this is the action of the function: whatever vector I put into the function will be filtered through 'is_odd" for TRUE (even numbers) or FALSE (odd numbers) elements; however the ! only allows the FALSE values to be subsetted, so it will only result in odd numbers
example_vector<-c(1,2,3,4,5,6,7,8,9)
odd_in(example_vector) #only the odd numbers are kept: 1, 3, 5, 7, 9
## [1] 1 3 5 7 9
#3A: Create a nested for loop, where the outer for() loop increments “a” 3 times, and the inner for() loop increments “b” 3 times. The nested loop prints the values of “a” and “b” as a vector.
for(a in seq(1,3, by=1)) { # this allows us to increment 'a' 3 times (start at 1, go up to 3 by 1 increment at a time)
for(b in seq(1,3, by=1)) { # this allows us to increment 'b' 3 times (start at 1, go up to 3 by 1 increment at a time)
print(c(a,b)) #this combines the values from a & b into a vector
}
}
## [1] 1 1
## [1] 1 2
## [1] 1 3
## [1] 2 1
## [1] 2 2
## [1] 2 3
## [1] 3 1
## [1] 3 2
## [1] 3 3
#3B: Create a while loop that prints out numbers drawn from the standard normal distribution (use rnorm()) but stops (breaks) if you get a number bigger than 1.
while(TRUE) {
number<-rnorm(1)
print(number)
if(number >=1) {
break
}
}
## [1] -1.859611
## [1] -1.543819
## [1] -1.144529
## [1] 0.006816586
## [1] -0.311281
## [1] 0.132057
## [1] 0.2852689
## [1] 0.160995
## [1] 1.058743
#while (TRUE) creates an infinite loop that can be only ended when there is a break in the function/loop
# rnorm(1) draws one random value from a standard normal distribution at a given time
#print(number) = prints the random number from standard normal distribution
# if (number>=1) means that once the function produces a value greater than or equal to 1, the loop is terminated from producing any more numbers
#3C: Using the following variables [a) msg <c(“PSY 290”) and b) i<-1] to create a a repeat loop that breaks off the incrementation of “i” after 6 iterations, and prints “msg” at every increment.
msg<- c("PSY 290") #message we want to print repeatedly
i <- 1 #setting the initial number of messages at 1
repeat {
print(msg) #allows us to print the statement
i<- i+1 #this tells us to continue printing the statement (ex: once, twice, thrice..)
if(i>6) { #but this ensures we will never print the message more than 6 times
break
}
}
## [1] "PSY 290"
## [1] "PSY 290"
## [1] "PSY 290"
## [1] "PSY 290"
## [1] "PSY 290"
## [1] "PSY 290"