“Practically, you’ll use the mode property often to convert e.g. a character vector to a numeric vector or vice versa. Before proceeding, first read section 3.1 of An Introduction to R, and the help page for the mode function.”
2.1.1 What is the mode of the following objects? First write down the mode, without using R. Then confirm using an approriate R command.
# c('a', 'b', 'c') <- characters
mode(c("a","b","c"))
## [1] "character"
# 3.32e16 <- numeric
mode(3.32e16)
## [1] "numeric"
# 1/3 <- numeric
mode(1/3)
## [1] "numeric"
# sqrt(-2i) <- complex number
mode(sqrt(-2i))
## [1] "complex"
#typeof(x) also works for each btw.
2.1.2 What is the mode of the following objects? First, enter the name of the object at the prompt (R will show its contents), and try to infer the mode from what you see. Then enter an R command, such that R will print the mode on the screen.
pressure # a list with 2 columns & 19 rows.
## temperature pressure
## 1 0 0.0002
## 2 20 0.0012
## 3 40 0.0060
## 4 60 0.0300
## 5 80 0.0900
## 6 100 0.2700
## 7 120 0.7500
## 8 140 1.8500
## 9 160 4.2000
## 10 180 8.8000
## 11 200 17.3000
## 12 220 32.1000
## 13 240 57.0000
## 14 260 96.0000
## 15 280 157.0000
## 16 300 247.0000
## 17 320 376.0000
## 18 340 558.0000
## 19 360 806.0000
lm #is used to fit linear models.
## function (formula, data, subset, weights, na.action, method = "qr",
## model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
## contrasts = NULL, offset, ...)
## {
## ret.x <- x
## ret.y <- y
## cl <- match.call()
## mf <- match.call(expand.dots = FALSE)
## m <- match(c("formula", "data", "subset", "weights", "na.action",
## "offset"), names(mf), 0L)
## mf <- mf[c(1L, m)]
## mf$drop.unused.levels <- TRUE
## mf[[1L]] <- quote(stats::model.frame)
## mf <- eval(mf, parent.frame())
## if (method == "model.frame")
## return(mf)
## else if (method != "qr")
## warning(gettextf("method = '%s' is not supported. Using 'qr'",
## method), domain = NA)
## mt <- attr(mf, "terms")
## y <- model.response(mf, "numeric")
## w <- as.vector(model.weights(mf))
## if (!is.null(w) && !is.numeric(w))
## stop("'weights' must be a numeric vector")
## offset <- as.vector(model.offset(mf))
## if (!is.null(offset)) {
## if (length(offset) != NROW(y))
## stop(gettextf("number of offsets is %d, should equal %d (number of observations)",
## length(offset), NROW(y)), domain = NA)
## }
## if (is.empty.model(mt)) {
## x <- NULL
## z <- list(coefficients = if (is.matrix(y)) matrix(, 0,
## 3) else numeric(), residuals = y, fitted.values = 0 *
## y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w !=
## 0) else if (is.matrix(y)) nrow(y) else length(y))
## if (!is.null(offset)) {
## z$fitted.values <- offset
## z$residuals <- y - offset
## }
## }
## else {
## x <- model.matrix(mt, mf, contrasts)
## z <- if (is.null(w))
## lm.fit(x, y, offset = offset, singular.ok = singular.ok,
## ...)
## else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
## ...)
## }
## class(z) <- c(if (is.matrix(y)) "mlm", "lm")
## z$na.action <- attr(mf, "na.action")
## z$offset <- offset
## z$contrasts <- attr(x, "contrasts")
## z$xlevels <- .getXlevels(mt, mf)
## z$call <- cl
## z$terms <- mt
## if (model)
## z$model <- mf
## if (ret.x)
## z$x <- x
## if (ret.y)
## z$y <- y
## if (!qr)
## z$qr <- NULL
## z
## }
## <bytecode: 0x7ffe200434d0>
## <environment: namespace:stats>
rivers #list of numeric values
## [1] 735 320 325 392 524 450 1459 135 465 600 330 336 280 315
## [15] 870 906 202 329 290 1000 600 505 1450 840 1243 890 350 407
## [29] 286 280 525 720 390 250 327 230 265 850 210 630 260 230
## [43] 360 730 600 306 390 420 291 710 340 217 281 352 259 250
## [57] 470 680 570 350 300 560 900 625 332 2348 1171 3710 2315 2533
## [71] 780 280 410 460 260 255 431 350 760 618 338 981 1306 500
## [85] 696 605 250 411 1054 735 233 435 490 310 460 383 375 1270
## [99] 545 445 1885 380 300 380 377 425 276 210 800 420 350 360
## [113] 538 1100 1205 314 237 610 360 540 1038 424 310 300 444 301
## [127] 268 620 215 652 900 525 246 360 529 500 720 270 430 671
## [141] 1770
mode(pressure)
## [1] "list"
mode(lm)
## [1] "function"
mode(rivers)
## [1] "numeric"
2.1.3 Consider the following list: x <- list(LETTERS, TRUE, print(1:10), print, 1:10)
What is the mode of x, and each of its elements? First write down the mode, without using R. Then confirm using the appropriate R commands.
x <- list(LETTERS, TRUE, print(1:10), print, 1:10)
## [1] 1 2 3 4 5 6 7 8 9 10
x # x is a list including mixed type of values. As you can see, the first element of the list is the letters - characters, 2nd is a logical expression, 3rd is a printed list of numbers. Print is a function to print and the last one is the numbers from 1 to 10 clearly.
## [[1]]
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] 1 2 3 4 5 6 7 8 9 10
##
## [[4]]
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x7ffe1dca2600>
## <environment: namespace:base>
##
## [[5]]
## [1] 1 2 3 4 5 6 7 8 9 10
mode(x) # a mixed list
## [1] "list"
mode(x[[1]]) # list of characters
## [1] "character"
mode(x[[2]]) # logical expression
## [1] "logical"
mode(x[[3]]) # list of numbers
## [1] "numeric"
mode(x[[4]]) # a function of r
## [1] "function"
mode(x[[5]]) # list of numbers again
## [1] "numeric"
sapply(x, mode) # easier way to apply same function to each element of the list.
## [1] "character" "logical" "numeric" "function" "numeric"
2.1.4 Show whether the vector x <- 1:100 is of mode numeric (TRUE) or not (FALSE).
x <- 1:100
is.numeric(x) # this is how we get a logical output for such question.
## [1] TRUE
2.1.5 Change the mode of the vector x <- 1:100 to character, with and without using the mode function. Write down the first 5 elements of the vector, after the mode conversion. Check your answer by printing the first 5 characters on the screen.
x <- as.character(x) # you can also use mode(x) <- 'character'
x[1:5] # when you print the first five elements, quotation marks show that these are characters, not numbers.
## [1] "1" "2" "3" "4" "5"
2.1.6 Change the mode of the character vector you created in the previous exercise, back to numeric. Again, with and without using the mode function.
#okaaaay
x <- as.numeric(x)
mode(x) <- "numeric"
2.1.7 Change the mode of the vector x <- c(‘1’, ‘2’, ‘three’) to numeric. First write down the new vector x, without using R, then check your answer using R.
# r does not recognize 'three' as a numeric element. so it will not change it to 3. instead, it will be NA in new vector.
x <- c('1', '2', 'three')
mode(x) <- "numeric"
## Warning in `mode<-`(`*tmp*`, value = "numeric"): NAs introduced by coercion
x # see what happened to "three"
## [1] 1 2 NA
2.1.8 Change the mode of the vector x <- c(TRUE, TRUE, FALSE, TRUE) to numeric. First write down the new vector x, without using R, then check your answer using R.
# TRUE = 1, FALSE = 0.
x <- c(TRUE, TRUE, FALSE, TRUE)
x <- as.numeric(x)
x
## [1] 1 1 0 1
2.1.9 Consider the vector x <- c(‘1’, ‘2’, ‘three’). What is the mode of y <- x + 1. First write down your answer without using R, then check using R.
# ERROR. you cannot use a numeric argument with a character vector. you need to make x numeric before doing an arithmetic operation.
x <- c('1', '2', 'three')
# y <- x + 1
x <- as.numeric(x)
## Warning: NAs introduced by coercion
# now y <- x + 1 works and y would be a numeric vector.
2.1.10 Create a vector y <- c(‘2’, ‘4’, ‘6’) from the vector x <- c(‘1’, ‘2’, ‘3’).
y <- c('2', '4', '6')
x <- c('1', '2', '3') #both are character vectors so we cannot use mathematical operations. so, we need to make x a numeric vector, do the math and make the new vector character again.
y <- as.numeric(x)*2
y <- as.character(y)
y
## [1] "2" "4" "6"