| @@ -0,0 +1,31 @@ | |||||
| NUMERIC_TYPES <- c("double", "integer") | |||||
| lsq <- function(X, y) { | |||||
| # TODO | |||||
| stopifnot("X may not be empty" = length(X) > 0) | |||||
| stopifnot("y may not be empty" = length(y) > 0) | |||||
| stopifnot("X must be numeric" = typeof(X) %in% NUMERIC_TYPES) | |||||
| stopifnot("y must be numeric" = typeof(y) %in% NUMERIC_TYPES) | |||||
| stopifnot("X must be matrix" = is.matrix(X)) | |||||
| stopifnot("y must be vector or matrix with one column" = is.vector(y) || (is.matrix(y) && ncol(y) == 1)) | |||||
| stopifnot("dimensions of X and y do not fit" = (is.vector(y) && nrow(X) == length(y)) || (is.matrix(y) && nrow(y) == nrow(X))) | |||||
| stopifnot("y may not contain NA" = all(!is.na(y))) | |||||
| stopifnot("X may not contain NA" = all(!is.na(X))) | |||||
| A <- t(X) %*% X | |||||
| stopifnot("det(t(X) %*% X) must not be zero" = det(A) != 0) | |||||
| solve(A, t(X) %*% y) | |||||
| } | |||||
| lsq(matrix(1:6, nrow=3), 1:3) | |||||
| lsq(matrix(runif(6), nrow=3), matrix(runif(3), ncol=1)) | |||||
| lsq(matrix(letters[1:6] , nrow=2), 1:3) | |||||
| lsq(matrix(1:6, nrow=3), list(1,2,3)) | |||||
| lsq(1:6, 1:3) | |||||
| lsq(matrix(1:6, nrow=3), array(1:3, dim=c(1,1,3))) | |||||
| lsq(matrix(1:6, nrow=3), 1:4) | |||||
| lsq(matrix(1:6, nrow=3), matrix(1:3, nrow=1)) | |||||
| lsq(matrix(1:6, nrow=3), matrix(1:6, nrow=3)) | |||||
| lsq(matrix(double(0), nrow=0, ncol=0), matrix(double(0), nrow=0, ncol=0)) | |||||
| lsq(matrix(1:6, nrow=3), c(1,NA,3)) | |||||
| lsq(matrix(c(1:5, NA), nrow=3), 1:3) | |||||
| lsq(matrix(c(1,1,2,1,1,2), nrow=3), 1:3) | |||||
| @@ -0,0 +1,35 @@ | |||||
| my_matrix <- function(vec, nrow=NULL, ncol=NULL, colnames=NULL, rownames=NULL) { | |||||
| stopifnot("at least one of nrow or ncol has to be specified" = !is.null(nrow) || !is.null(ncol)) | |||||
| if (is.null(nrow)) { | |||||
| stopifnot("incompatible length" = length(vec) %% ncol == 0) | |||||
| nrow <- length(vec) / ncol | |||||
| } else if (is.null(ncol)) { | |||||
| stopifnot("incompatible length" = length(vec) %% nrow == 0) | |||||
| ncol <- length(vec) / nrow | |||||
| } else if (length(vec) == 1) { | |||||
| vec <- rep(vec, nrow * ncol) | |||||
| } else stopifnot("incompatible length" = length(vec) == nrow * ncol) | |||||
| dim(vec) <- c(nrow, ncol) | |||||
| stopifnot("lenght of colnames must be ncol" = is.null(colnames) || length(colnames) == ncol) | |||||
| stopifnot("lenght of rownames must be nrow" = is.null(rownames) || length(rownames) == nrow) | |||||
| dimnames(vec) <- list(rownames, colnames) | |||||
| return(vec) | |||||
| } | |||||
| my_matrix(1:6) | |||||
| my_matrix(1:6, ncol=1) | |||||
| my_matrix(1:6, ncol=2) | |||||
| my_matrix(1:6, ncol=3) | |||||
| my_matrix(1:6, ncol=6) | |||||
| my_matrix(1:6, ncol=4) | |||||
| my_matrix(1:6, nrow=2) | |||||
| my_matrix(1:6, nrow=7) | |||||
| my_matrix(1:6, ncol=2, nrow=2) | |||||
| my_matrix(1:6, ncol=2, nrow=3) | |||||
| my_matrix(1:6, ncol=2, nrow=1) | |||||
| my_matrix(0, ncol=3, nrow=2) | |||||
| my_matrix(1:6, ncol=3, colnames=LETTERS[1:3]) | |||||
| my_matrix(1:6, ncol=3, colnames=LETTERS[1:2]) | |||||
| my_matrix(1:6, ncol=3, rownames=letters[24 + 1:2]) | |||||
| my_matrix(1:6, ncol=3, colnames=LETTERS[1:3], rownames=letters[24 + 1:2]) | |||||
| @@ -0,0 +1,22 @@ | |||||
| my_tibble <- function(data) { | |||||
| attr(data, "row.names") <- (1:lengths(data)[1]) | |||||
| attr(data, "class") <- c("tbl_df", "tbl", "data.frame") | |||||
| return(data) | |||||
| } | |||||
| library(tibble) | |||||
| my_tb <- my_tibble(list(x=1:3, y=letters[1:3])) | |||||
| tb <- tibble(x=1:3 , y=letters[1:3]) | |||||
| identical(tb, my_tb) | |||||
| my_factor <- function(data) { | |||||
| lvls <- unique(data) | |||||
| vec <- sapply(data, function(x) match(x, lvls), USE.NAMES=FALSE) | |||||
| attr(vec, "levels") <- lvls | |||||
| attr(vec, "class") <- "factor" | |||||
| return(vec) | |||||
| } | |||||
| my_fac <- my_factor (c("a", "b", "a", "a", "c", "c")) | |||||
| fac <- factor(c("a", "b", "a", "a", "c", "c")) | |||||
| identical(fac, my_fac) | |||||
| @@ -0,0 +1,56 @@ | |||||
| collatz <- function(raw_x, max_iter) { | |||||
| stopifnot("Length of x must be 1" = length(raw_x) == 1) | |||||
| x <- as.integer(raw_x) | |||||
| stopifnot("Cannot interprete x as integer" = !is.na(x)) | |||||
| sequ <- c(x) | |||||
| len <- NA | |||||
| for (n in (1:(max_iter-1))) { | |||||
| if (x == 1) { | |||||
| len <- n | |||||
| break | |||||
| } else if (x %% 2 == 0) { | |||||
| x <- x / 2 | |||||
| } else { | |||||
| x <- 3*x+1 | |||||
| } | |||||
| sequ <- c(sequ, x) | |||||
| } | |||||
| return(list(seq=sequ, len=len)) | |||||
| } | |||||
| str(collatz(1, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int 1 | |||||
| ## $ len: int 1 | |||||
| str(collatz(2, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:2] 2 1 | |||||
| ## $ len: int 2 | |||||
| str(collatz(3, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:8] 3 10 5 16 8 4 2 1 | |||||
| ## $ len: int 8 | |||||
| str(collatz(3, 5)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:5] 3 10 5 16 8 | |||||
| str(collatz("4", 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:3] 4 2 1 | |||||
| ## $ len: int 3 | |||||
| str(collatz("four", 1e4)) | |||||
| ## Warning in collatz("four", 10000): NAs introduced by coercion | |||||
| ## Error in collatz("four", 10000): Cannot interprete x as integer | |||||
| str(collatz(1:5, 1e4)) | |||||
| ## Error in collatz(1:5, 10000): Length of x must be 1 | |||||
| str(collatz(5.0, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:6] 5 16 8 4 2 1 | |||||
| ## $ len: int 6 | |||||
| str(collatz(5.1, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:6] 5 16 8 4 2 1 | |||||
| ## $ len: int 6 | |||||
| str(collatz(5.9, 1e4)) | |||||
| ## List of 2 | |||||
| ## $ seq: int [1:6] 5 16 8 4 2 1 | |||||
| ## $ len: int 6 | |||||