Define a function plot_red as a wrapper function to plot that changes the default color to red (col = "red").
#plot_red(1:10)
plot_red = function(x, y){
pl = plot(x, y, col = "red")
pl
}
Model y below as a linear function of x. Show that the model predicts 19.54463 when x is 20.
x = 1:10
y = x + sin(x)
fit = lm(y~x)
x2 = data.frame(x = seq(from = 1, to = 20))
yhat= predict(fit, x2)
yhat
## 1 2 3 4 5 6 7 8
## 1.326236 2.285099 3.243961 4.202824 5.161687 6.120550 7.079413 8.038276
## 9 10 11 12 13 14 15 16
## 8.997139 9.956002 10.914865 11.873728 12.832591 13.791454 14.750317 15.709180
## 17 18 19 20
## 16.668043 17.626906 18.585769 19.544632
The following code creates a directory containing several CSV files.
Read all of these files in and combine them into one single data frame, d. If you do it correctly, d will have 30 rows. You can do this idiomatically in three lines of code.
datadir = "letters_for_stat128_final"
data_all = list.files(path = datadir, pattern = "*.csv", full.names = TRUE)
readdata = lapply(data_all, read.csv)
#merge_all = lapply(readdata, merge)
#So I can merge this the long way but I wasn't sure how to merge using lappy.
Extract the 10 digit phone numbers from the following character vector.
s = c("My number is 593-461-0728"
, "My address is 123 Fake Street, and my phone number is 578-163-4290, thanks."
, "Either call 269-501-3748 or email foo@bar.com.")
goal = c("593-461-0728", "578-163-4290", "269-501-3748")
e = gsub("[ ,.@)(+]|[a-zA-Z]*:?", "" , s)
e
## [1] "593-461-0728" "123578-163-4290" "269-501-3748"
#almost
Define a new print method for objects of class final that accepts any arguments and prints out We're done!. Hint: use cat().
s = "This is it?"
class(s) = c("final", class(s))
s
## [1] "This is it?"
## attr(,"class")
## [1] "final" "character"
#Technically this new `s` overwrites the previous s. So this works too?
s = print("We're done!")
## [1] "We're done!"
Hereโs the behavior you should see in the console after you define your method:
> s
We're done!
>