-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path11_biz_S3.R
60 lines (44 loc) · 1.01 KB
/
11_biz_S3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## snippets on slides created with lots of selective reprex()ing and toggling of
## the chunk options
#+ include = FALSE
str_reverse <- function(x) {
vapply(
strsplit(x, ""),
FUN = function(z) paste(rev(z), collapse = ""),
FUN.VALUE = "")
}
#+ include = FALSE
bizarro <- function(x) {
UseMethod("bizarro")
}
bizarro.default <- function(x) {
stop(
"Don't know how to make bizzaro <",
class(x)[[1]], ">",
call. = FALSE
)
}
#+ include = FALSE, eval = FALSE
bizarro(1:5)
bizarro(TRUE)
bizarro("abc")
#+ include = FALSE
bizarro.numeric <- function(x) -x
bizarro.logical <- function(x) !x
bizarro.character <- function(x) str_reverse(x)
bizarro.factor <- function(x) {
levels(x) <- rev(levels(x))
x
}
bizarro.data.frame <- function(x) {
names(x) <- bizarro(names(x))
x[] <- lapply(x, bizarro)
x
}
#+ include = TRUE
bizarro(1:5)
bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))
bizarro(c("abc", "def"))
(m <- factor(month.abb[1:3], levels = month.abb[1:3]))
bizarro(m)
bizarro(head(iris, 3))