library(lobstr)
1:5^2
1:5*2
mtcars[mtcars$cyl = 4, ]
mtcars[-1:4, ]
mtcars[mtcars$cyl <= 5]
mtcars[mtcars$cyl == 4 | 6, ]
c <- 10
c(c = c)
Q2: What does dim() return when applied to a 1-dimensional vector? When might you use NROW() or NCOL()?
a <- 1:10
b <- a
c <- b
d <- 1:10
Q5: Brainstorm as many ways as possible to extract the third value from the cyl variable in the mtcars dataset.
x <- 1:10
if (length(x)) "not empty" else "empty"
#> [1] "not empty"
x <- numeric()
if (length(x)) "not empty" else "empty"
#> [1] "empty"
xs <- c(1, 2, 3)
for (x in xs) {
xs <- c(xs, x * 2)
}
xs
#> [1] 1 2 3 2 4 6
#>
for (i in 1:3) {
i <- i * 2
print(i)
}
#> [1] 2
#> [1] 4
#> [1] 6
#>
Q9: The following code accesses the mean function in multiple ways. Do they all point to the same underlying function object? Verify this with lobstr::obj_addr().
mean
base::mean
get("mean")
evalq(mean)
match.fun("mean")
Q10: Test your knowledge of vector coercion rules by predicting the output of the following uses of c():
c(1, FALSE)
c("a", 1)
c(TRUE, 1L)
Q11: What sort of object does table() return? What is its type? What attributes does it have? How does the dimensionality change as you tabulate more variables?
f <- function(x) {
f <- function(x) {
f <- function() {
x ^ 2
}
f() + 1
}
f(x) * 2
}
f(10)
Q14: Write your own version of + that pastes its inputs together if they are character vectors but behaves as usual otherwise. In other words, make this code work:
1 + 2
#> [1] 3
"a" + "b"
#> [1] "ab"
#>