Chapter 3.1.1 Exercises

loop_fnc
In [22]:
i39 <- sapply(3:9, seq) # list of vectors
sapply(i39, fivenum)
1.01.01 1.01.01.01
1.51.52 2.02.52.53
2.02.53 3.54.04.55
2.53.54 5.05.56.57
3.04.05 6.07.08.09
In [7]:
vapply(i39, fivenum,
c(Min. = 0, "1st Qu." = 0, Median = 0, "3rd Qu." = 0, Max. = 0))
Min.1.01.01 1.01.01.01
1st Qu.1.51.52 2.02.52.53
Median2.02.53 3.54.04.55
3rd Qu.2.53.54 5.05.56.57
Max.3.04.05 6.07.08.09
In [63]:
# sapply(*, "array") -- artificial example

(v <- structure(10*(5:8), names = LETTERS[1:4]))

f2 <- function(x, y) outer(rep(x, length.out = 3), y)
(a2 <- sapply(v, f2, y = 2*(1:5), simplify = "array"))

# wtf?
A
50
B
60
C
70
D
80
ABCD
100120140160
100120140160
100120140160
200240280320
200240280320
200240280320
300360420480
300360420480
300360420480
400480560640
400480560640
400480560640
500600700800
500600700800
500600700800
In [14]:
a.2 <- vapply(v, f2, outer(1:3, 1:5), y = 2*(1:5))

stopifnot(dim(a2) == c(3,5,4), all.equal(a2, a.2),
identical(dimnames(a2), list(NULL,NULL,LETTERS[1:4])))

hist(replicate(100, mean(rexp(10))))
In [19]:
# However, we can do what we want to do by using mapply().

mapply(sumsq, 1:10, 1:10, MoreArgs = list(x = x))
  1. 223.829798225267
  2. 134.436004138874
  3. 116.850914987302
  4. 110.348278325997
  5. 107.179466861847
  6. 105.372246941011
  7. 104.230920575646
  8. 103.456708227138
  9. 102.903006726754
  10. 102.490577631973
In [5]:
mapply(function(x, y) seq_len(x) + y,
c(a = 1, b = 2, c = 3), # names from first
c(A = 10, B = 0, C = -10))
$a
11
$b
  1. 1
  2. 2
$c
  1. -9
  2. -8
  3. -7
In [6]:
word <- function(C, k) paste(rep.int(C, k), collapse = "")
utils::str(mapply(word, LETTERS[1:6], 6:1, SIMPLIFY = FALSE))
List of 6
 $ A: chr "AAAAAA"
 $ B: chr "BBBBB"
 $ C: chr "CCCC"
 $ D: chr "DDD"
 $ E: chr "EE"
 $ F: chr "F"
In [14]:
# Unfortunately, there are NAs in the data so we cannot simply take the means of those variables. However, we can tell the colMeans function to remove the NAs before computing the mean.

sapply(s, function(x) {
         colMeans(x[, c("Ozone", "Solar.R", "Wind")], 
                  na.rm = TRUE)
 })
56789
Ozone 23.61538 29.44444 59.115385 59.961538 31.44828
Solar.R181.29630 190.16667 216.483871171.857143167.43333
Wind 11.62258 10.26667 8.941935 8.793548 10.18000
In [16]:
# With multiple factors and many levels, creating an interaction can result in many levels that are empty.

str(split(x, list(f1, f2)))
List of 10
 $ 1.1: num [1:2] -1.342 -0.283
 $ 2.1: num(0) 
 $ 1.2: num [1:2] 1.757 0.654
 $ 2.2: num(0) 
 $ 1.3: num -0.34
 $ 2.3: num -1.51
 $ 1.4: num(0) 
 $ 2.4: num [1:2] 0.316 0.733
 $ 1.5: num(0) 
 $ 2.5: num [1:2] 2.089 -0.111
In [17]:
# We can also take the group means without simplifying the result, which will give us a list. For functions that return a single value, usually, this is not what we want, but it can be done.

tapply(x, f, mean, simplify = FALSE)
$`1`
0.199351661827255
$`2`
0.486338333622552
$`3`
1.04280692788798