--1-A quick refresher
#1-Writing a function in R
#2-Writing a function
#Using the function template, write a function ratio()
#that takes arguments x and y and returns their ratio, x / y.
#Check your function by calling it with the arguments 3 and 4.
# Define ratio() function
ratio <- function(x, y) {
cevi= x/y
return(cevi)
}
# Call ratio() with arguments 3 and 4
ratio(3,4)
#3-Arguments
#Here's a rather cryptic call to mean().
#Rewrite the call to follow the best practices we just discussed.
#Use the natural ordering of the arguments,
#which you can find by typing ?mean in the console.
# Rewrite the call to follow best practices
mean(c(1:9, NA), trim = 0.1, na.rm = TRUE)
#4-Function output
#Consider the following function that takes a numeric value, x, as input:
f <- function(x) {
if (TRUE) {
return(x + 1)
}
x
}
f(2)
#3->The right answer
#5-Environments
#6-Testing your understanding of scoping (1)
y <- 10
f <- function(x) {
x + y
}
f(10)
#What will f(10) return?
#The right answer is 20
#7-Testing your understanding of scoping (2)
y <- 10
f <- function(x) {
y <- 5
x + y
}
f(10)
#What will f(10) return?
#The right answer is 15
#8-Testing your understanding of scoping (3)
f <- function(x) {
y <- 5
x + y
}
f(5)
#Now, what will typing y return?
y
#9-Data structures
#Two type vector in R: Atomic (homogenious) and List (list can contain list:
#heterogenious)
#Typeof()
#10-Atomic types of vectors
#Which of these is NOT one of the atomic types of vectors in R?
# logical
# integer
# double
# character
# factor-> the right answer
#11-Subsetting lists
# What type of object is the...
# 2nd element in tricky_list?
# Element called x in tricky_list?
# 2nd element inside the element called x in tricky_list?
#new dataset #uncomplete
a<-c(1.1539613, -0.3511877, 0.6704161, -0.4547493, 0.7816482 ,-1.4728369,
1.7557698 ,-1.4971543 , 0.3650022 ,0.9538584)
b<-c(FALSE, FALSE, FALSE, FALSE ,FALSE, TRUE, TRUE ,TRUE, TRUE, TRUE)
c_<-c("hello!","hi!","goodbye!","bye!")
num<-list(a)
num
typeof(num)
y<-list(b)
y
typeof(y)
class(y)
x<-list(c_)
tricky_list<-list(num,y,x)
tricky_list
# 2nd element in tricky_list
typeof(tricky_list[[2]])
# Element called x in tricky_list
typeof(tricky_list[["x"]])
# 2nd element inside the element called x in tricky_list
typeof(tricky_list[["x"]][[2]])
#12-Exploring lists
# Use names() on tricky_list to guess where the regression model is stored.
# Use names() and str() on the model element of tricky_list.
# Extract the coefficients element of the model element of tricky_list.
# Extract the wt element of the coefficients element of the model element
#of tricky_list.
a<-c(1.1539613, -0.3511877, 0.6704161, -0.4547493, 0.7816482 ,-1.4728369,
1.7557698 ,-1.4971543 , 0.3650022 ,0.9538584)
b<-c(FALSE, FALSE, FALSE, FALSE ,FALSE, TRUE, TRUE ,TRUE, TRUE, TRUE)
c_<-c("hello!","hi!","goodbye!","bye!")
num<-list(a)
num
typeof(num)
y<-list(b)
y
typeof(y)
class(y)
x<-list(c_)
tricky_list<-list(num,y,x)
tricky_list
# Guess where the regression model is stored
names(tricky_list)
# Use names() and str() on the model element
names(tricky_list[["model"]])
str(tricky_list[["model"]])
# Subset the coefficients element
tricky_list[["model"]][["coefficients"]]
# Subset the wt element
tricky_list[["model"]][["coefficients"]][["wt"]]
#13-for loops
#14-A safer way to create the sequence
# A better method is to use the seq_along() function.
#This function generates a sequence along the index of the object passed to it,
# but handles the empty case much better.
# Replace the 1:ncol(df) sequence in our for loop with seq_along(df).
# Change the value of df to an empty data frame.
# Repeat the for loop to verify there is no error.
#new dataset
a<-c(1:5)
b<-c(5:1)
df<-data.frame(a,b)
df
# Replace the 1:ncol(df) sequence
for (i in seq_along(df)) {
print(median(df[[i]]))
}
# Change the value of df
df<-data.frame()
# Repeat for loop to verify there is no error
for (i in seq_along(df)) {
print(median(df[[i]]))
}
#15-Keeping output
# Create a new "double" vector of length ncol(df) and name it output.
# Inside the body of the loop, store the result of each
# iteration result in the ith element of output.
# Print output to the console.
#new dataset
a<-c(1:5)
b<-c(5:1)
df<-data.frame(a,b)
df
# Replace the 1:ncol(df) sequence
for (i in seq_along(df)) {
print(median(df[[i]]))
}
# Change the value of df
df<-data.frame()
df
# Create new double vector: output
output <- vector("double", ncol(df))
# Alter the loop
for (i in seq_along(df)) {
# Change code to store result in output
output[[i]] <- median(df[[i]])
}
# Print output
output
Last updated