1:10 c(4537426538) printid() printid <- function() { return(4537426538) } printnum <- function() { return(1:10) } printnum() convertFtoC <- function(f) { return(((f - 32)/9) * 5) } convertFtoC(77) convertFtoC <- function(f) { celcius <- ((f - 32)/9) * 5 return(celcius) } convertFtoC(50) freezingpoint <- 0 convertFtoC <- function(f) { celcius <- ((f - freezingpoint)/9) * 5 return(celcius) } convertFtoC(50) convertFtoC <- function(f) { celcius <- ((f - 32)/9) * 5 return(celcius) } convertFtoC <- function(f) { celcius <- ((f - 32)/9) * 5 celcius } convertFtoC <- function(f) ((f - 32)/9) * 5 product <- function(x, y) { return(x*y) } product(2, 3) product(2:5, 1:4) radical <- function(x, root = 2) { x^(1/root) } radical(4, root = 2) radical(4) radical(8, 3) radical(x = 81, root = 4) out <- lm(rating ~ complaints, data = attitude) summary(out)$r.squared rsquare1 <- function(obj) { summary(obj)$r.squared } rsquare1(out) library(psych) rsquare1(lm(SATQ ~ SATV, data = sat.act)) alpha <- function(dat) { p <- ncol(dat) term1 <- p / (p - 1) CM <- cov(dat) numerator <- sum(diag(CM)) sumscore <- apply(dat, 1, sum) denominator <- var(sumscore) term2 <- 1 - numerator/denominator alpha <- term1 * term2 return(alpha) } alpha(attitude) ########################### # If-statement ########################### x <- 5 y <- 1231321315 if(x > 0 & y > 0) { print("Both are positive numbers!!!") } x <- 0 if(x > 0) { print("Positive number!!!") } else { print("Nonpositive number!!!") } x <- -20 if(x > 0) { print("Positive number!!!") } else if (x == 0) { print("Zero!!!") } else { print("Negative number!!!") } score <- 88 grade <- NA if(score >= 90) { grade <- "A" } else if (score >= 80) { grade <- "B" } else if (score >= 70) { grade <- "C" } else if (score >= 60) { grade <- "D" } else { grade <- "F" } if(score >= 90) { grade <- "A" } else if (score < 90 & score >= 80) { grade <- "B" } else if (score < 80 & score >= 70) { grade <- "C" } else if (score < 70 & score >= 60) { grade <- "D" } else { grade <- "F" } scores <- 0:100 grades <- rep(NA, length(scores)) grades <- rep("F", length(scores)) grades[scores >= 60] <- "D" grades[scores >= 70] <- "C" grades[scores >= 80] <- "B" grades[scores >= 90] <- "A" out rsquare2 <- function(obj, adjusted = FALSE) { if(adjusted == TRUE) { return(summary(obj)$adj.r.squared) } else { return(summary(obj)$r.squared) } } rsquare2(out, TRUE) rsquare2(out, FALSE) simplescatter <- function(x, y, fitline = FALSE) { plot(x, y) if(fitline) abline(lm(y ~ x)) } simplescatter(attitude$complaints, attitude$rating, fitline = TRUE) descriptive <- function(x, miss) { c(mean = mean(x, na.rm = miss), sd = sd(x, na.rm = miss)) } apply(attitude, 2, descriptive, miss = TRUE) aggregate(SATQ ~ gender, data = sat.act, FUN = descriptive, miss = TRUE) ### Boot library(boot) out <- lm(rating ~ complaints, data = attitude) summary(out)$r.squared FUN <- function(dat, i) { out <- lm(rating ~ complaints, data = dat[i,]) summary(out)$r.squared } bootout <- boot(attitude, FUN, stype = "i", R = 10000) ci <- boot.ci(bootout, conf = 0.95, type = c("perc", "bca")) FUN2 <- function(dat, i) { cor(dat[i, 1], dat[i, 2]) - cor(dat[i, 1], dat[i, 3]) } bootout <- boot(attitude, FUN2, stype = "i", R = 1000) ci <- boot.ci(bootout, conf = 0.95, type = c("perc", "bca")) ############################# ### Debugging ############################# # Problematic function 1 addition <- function(a, b) a + b # Note that a and b could be non-numeric. It causes the function to be broken. # Use the try function to allow function to run even though a line will cause an error addition <- function(a, b) { result <- NA try(result <- a + b) result } # The result of the try function can be saved. If the command inside the try function causes an error, the result will be in a "try-error" object. We can provide an appropriate error message. addition <- function(a, b) { result <- NA tryout <- try(result <- a + b) if(is(tryout, "try-error")) { print("Two things cannot be added.") return(NA) } else { return(result) } } # Try catch is a shorten version of the previous one. addition <- function(a, b) { result <- NA tryCatch(result <- a + b, error = function(e) print("Two things cannot be added.")) result } # Check whether a and b are numeric beforehand. The warning function is to provide a warning message but not breaking a function. addition <- function(a, b) { if(!is.numeric(a)) warning("a is not numeric") if(!is.numeric(b)) warning("b is not numeric") result <- NA tryCatch(result <- a + b, error = function(e) print("Two things cannot be added together.")) result } # The stop function is to provide a warning message and break a function. addition <- function(a, b) { if(!is.numeric(a)) stop("a is not numeric") if(!is.numeric(b)) stop("b is not numeric") result <- NA tryCatch(result <- a + b, error = function(e) print("Two things cannot be added together.")) result } # The traceback function is used to trace back the origin of the error message. traceback() # The browser function can be added in a function to debug an error. Check in the internet. browser()