R Under development (unstable) (2023-12-09 r85665 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("matrixStats") > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # Setup > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > data <- data.frame( + logical = c(TRUE, FALSE, TRUE), + integer = 1:3, + double = seq(from = 1.0, to = 3.0, by = 1.0), + complex = seq(from = 1.0, to = 3.0, by = 1.0) + 1.0i, + character = letters[1:3], + stringsAsFactors = FALSE + ) > > modes <- names(data) > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # Special cases > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > cat("NULL...\n") NULL... > stopifnot(identical(anyMissing(NULL), FALSE)) > cat("NULL...done\n") NULL...done > > cat("raw...\n") raw... > stopifnot(identical(anyMissing(as.raw(0:2)), FALSE)) > cat("raw...done\n") raw...done > > cat("list(NULL)...\n") list(NULL)... > stopifnot(identical(anyMissing(list(NULL)), FALSE)) > cat("list(NULL)...done\n") list(NULL)...done > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # Scalars, vectors, and matrices of various modes > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > for (mode in modes) { + cat(sprintf("Mode: %s...\n", mode)) + values <- data[[mode]] + + # Scalars + cat(" scalar\n") + x <- values[1L] + print(x) + stopifnot(identical(anyMissing(x), FALSE)) + is.na(x) <- TRUE + print(x) + stopifnot(identical(anyMissing(x), TRUE)) + + # Vectors + cat(" vector\n") + x <- values + print(x) + stopifnot(identical(anyMissing(x), FALSE)) + is.na(x)[2L] <- TRUE + print(x) + stopifnot(identical(anyMissing(x), TRUE)) + + # Matrices + cat(" matrix\n") + x <- matrix(c(values, values), ncol = 2L) + print(x) + stopifnot(identical(anyMissing(x), FALSE)) + is.na(x)[2L] <- TRUE + print(x) + stopifnot(identical(anyMissing(x), TRUE)) + + cat(sprintf("Mode: %s...done\n", mode)) + } # for (mode ...) Mode: logical... scalar [1] TRUE [1] NA vector [1] TRUE FALSE TRUE [1] TRUE NA TRUE matrix [,1] [,2] [1,] TRUE TRUE [2,] FALSE FALSE [3,] TRUE TRUE [,1] [,2] [1,] TRUE TRUE [2,] NA FALSE [3,] TRUE TRUE Mode: logical...done Mode: integer... scalar [1] 1 [1] NA vector [1] 1 2 3 [1] 1 NA 3 matrix [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [,1] [,2] [1,] 1 1 [2,] NA 2 [3,] 3 3 Mode: integer...done Mode: double... scalar [1] 1 [1] NA vector [1] 1 2 3 [1] 1 NA 3 matrix [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [,1] [,2] [1,] 1 1 [2,] NA 2 [3,] 3 3 Mode: double...done Mode: complex... scalar [1] 1+1i [1] NA vector [1] 1+1i 2+1i 3+1i [1] 1+1i NA 3+1i matrix [,1] [,2] [1,] 1+1i 1+1i [2,] 2+1i 2+1i [3,] 3+1i 3+1i [,1] [,2] [1,] 1+1i 1+1i [2,] NA 2+1i [3,] 3+1i 3+1i Mode: complex...done Mode: character... scalar [1] "a" [1] NA vector [1] "a" "b" "c" [1] "a" NA "c" matrix [,1] [,2] [1,] "a" "a" [2,] "b" "b" [3,] "c" "c" [,1] [,2] [1,] "a" "a" [2,] NA "b" [3,] "c" "c" Mode: character...done > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # Data frames > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > cat("data.frame...\n") data.frame... > x <- data > stopifnot(identical(anyMissing(x), FALSE)) > for (mode in modes) { + x <- data + is.na(x[[mode]])[2L] <- TRUE + print(x) + stopifnot(identical(anyMissing(x), TRUE)) + } # for (mode ...) logical integer double complex character 1 TRUE 1 1 1+1i a 2 NA 2 2 2+1i b 3 TRUE 3 3 3+1i c logical integer double complex character 1 TRUE 1 1 1+1i a 2 FALSE NA 2 2+1i b 3 TRUE 3 3 3+1i c logical integer double complex character 1 TRUE 1 1 1+1i a 2 FALSE 2 NA 2+1i b 3 TRUE 3 3 3+1i c logical integer double complex character 1 TRUE 1 1 1+1i a 2 FALSE 2 2 NA b 3 TRUE 3 3 3+1i c logical integer double complex character 1 TRUE 1 1 1+1i a 2 FALSE 2 2 2+1i 3 TRUE 3 3 3+1i c > cat("data.frame...done\n") data.frame...done > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # Lists > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > cat("list...\n") list... > x <- as.list(data) > stopifnot(identical(anyMissing(x), FALSE)) > for (mode in modes) { + x <- as.list(data) + is.na(x[[mode]])[2L] <- TRUE + print(x) + stopifnot(identical(anyMissing(x), TRUE)) + } # for (mode ...) $logical [1] TRUE NA TRUE $integer [1] 1 2 3 $double [1] 1 2 3 $complex [1] 1+1i 2+1i 3+1i $character [1] "a" "b" "c" $logical [1] TRUE FALSE TRUE $integer [1] 1 NA 3 $double [1] 1 2 3 $complex [1] 1+1i 2+1i 3+1i $character [1] "a" "b" "c" $logical [1] TRUE FALSE TRUE $integer [1] 1 2 3 $double [1] 1 NA 3 $complex [1] 1+1i 2+1i 3+1i $character [1] "a" "b" "c" $logical [1] TRUE FALSE TRUE $integer [1] 1 2 3 $double [1] 1 2 3 $complex [1] 1+1i NA 3+1i $character [1] "a" "b" "c" $logical [1] TRUE FALSE TRUE $integer [1] 1 2 3 $double [1] 1 2 3 $complex [1] 1+1i 2+1i 3+1i $character [1] "a" NA "c" > cat("list...done\n") list...done > > proc.time() user system elapsed 0.17 0.07 0.21