-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions-homework.R
118 lines (82 loc) · 3.8 KB
/
functions-homework.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#PSYC 259 Homework 4 - Writing functions
#For full credit, answer at least 6/8 questions
#List names of students collaborating with:
### SETUP: RUN THIS BEFORE STARTING ----------
library(tidyverse)
set.seed(1)
id <- rep("name", 30)
x <- runif(30, 0, 10)
y <- runif(30, 0, 10)
z <- runif(30, 0, 10)
ds <- tibble(id, x, y, z)
### Question 1 ----------
#Vectors x, y, and z contain random numbers between 1 and 10.
#Write a function called "limit_replace" that will replace values less than 2 or greater than 8 with NA
#Then, run the function on x and save the results to a new vector "x_replace" to show it worked
### Question 2 ----------
#Make a new version of limit_replace that asks for arguments for a lower bounary and an upper boundary
#so that they can be customized (instead of being hard coded as 2 and 8)
#Run the function on vector y with boundaries 4 and 6, saving the results to a new vector "y_replace"
### Question 3 ----------
#Write a function called "plus_minus_SD" that can take one of the vectors (x/y/z) as input
#and "num_of_SDs" as an input and returns the boundaries +/- num_of_SDs around the mean.
#plus_minus_SD(x, 1) would give +/- 1SD around the mean of x, plus_minus_SD(y, 2) would give +/- 2SDs around the mean
#Make num_of_SDs default to 1
#run the new function on x, y, and z with 1 SD
### Question 4 ----------
#Write an another new version of limit_replace
#This time, make the upper and lower boundaries optional arguments
#If they are not given, use +/- 1 SD as the boundaries (from your plus_minus_SD function)
#Apply the function to each column in ds, and save the results to a new tibble called "ds_replace"
### Question 5 ----------
#Add a "stopifnot" command to your limit_replace function to make sure it only runs on numeric variables
#Try running it on a non-numeric input (like "id") to make sure it gives you an error
### Question 6 ----------
#What other requirements on the input do you need to make the function work correctly?
#Add another stopifnot to enforce one more requirement
### Question 7 ----------
#Clear out your workspace and load the built-in diamonds dataset by running the lines below
#RUN THIS CODE
rm(list = ls())
library(tidyverse)
ds_diamonds <- diamonds
#Save your two functions to an external file (or files)
#Then, load your functions from the external files(s)
#Next, run your limit_replace function on all of the numeric columns in the new data set
#and drop any rows with NA, saving it to a new tibble named "ds_trimmed"
### Question 8 ----------
#The code below makes graphs of diamond price grouped by different variables
#Refactor it to make it more efficient using functions and/or iteration
#Don't worry about the order of the plots, just find a more efficient way to make all 6 plots
#Each cut (Premium/Ideal/Good) should have a plot with trimmed and untrimmed data
#The title of each plot should indicate which cut and whether it's all vs. trimmed data
ds_diamonds %>% filter(cut == "Premium") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Premium, all") +
theme_minimal()
ds_diamonds %>% filter(cut == "Ideal") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Ideal, all") +
theme_minimal()
ds_diamonds %>% filter(cut == "Good") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Good, all") +
theme_minimal()
ds_trimmed %>% filter(cut == "Premium") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Premium, trimmed") +
theme_minimal()
ds_trimmed %>% filter(cut == "Ideal") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Ideal, trimmed") +
theme_minimal()
ds_trimmed %>% filter(cut == "Good") %>%
ggplot(aes(x = clarity, y = price)) +
geom_boxplot() +
ggtitle("Good, trimmed") +
theme_minimal()