somefunction <- function (...)
{
k <- length(ll <- list(...))
if (k == 0L)
return(invisible())
mc <- match.call()
for (i in 1L:k) if (!(is.logical(r <- ll[[i]]) && !any(is.na(r)) &&
all(r))) {
ch <- deparse(mc[[i + 1]], width.cutoff = 60L)
if (length(ch) > 1L)
ch <- paste(ch[1L], "....")
stop(paste(ch, " is not ", if (length(r) > 1L)
"all ", "TRUE", sep = ""), call. = FALSE)
}
invisible()
}
somefunction(mtcars)
# Your Turn
larger <- function(x, y) {
y.is.bigger <- y > x
x[y.is.bigger] <- y[y.is.bigger]
x
}
larger(c(1, 5, 10), c(2, 4, 11))
larger(c(1, 5, 10), 6)
larger(c(1, 5, 10), c(11, 6))
# Your Turn - Discussion
larger <- function(x, y) {
len_x <- length(x)
len_y <- length(y)
if (len_x != len_y) {
# warn in case one vector is not a multiple of the other in length
shortie <- min(len_x, len_y)
longer <- max(len_x, len_y)
if (longer %% shortie != 0)
warning("one vector is not a multiple of the other in length")
if (len_y < len_x) y <- rep(y, length.out = len_x)
if (len_x < len_y) x <- rep(x, length.out = len_y)
}
y.is.bigger <- y > x
x[y.is.bigger] <- y[y.is.bigger]
x
}
stopifnot(is.character(1:10))
library(assertthat)
assert_that(is.character(1:10))
col_means <- function(df) {
numeric <- sapply(df, is.numeric)
numeric_cols <- df[, numeric]
data.frame(lapply(numeric_cols, mean))
}Code from class
code
Week07