9.1 Exercices
stopifnot : everything need to be
TRUE
if
returns an expresions (“do something”) related to a condition (TRUE/FALSE)attributes<-
(x, NULL): Value of x: no but remove it’s attributes[
and[<-
when called in higher order function (Map, lapply)%someting%
, only element 1 and element 2Use n parameters, pass other argument to a function
Nothing it is just that they make sense only in a data specific context
I guess the idea here is to say that the second argument will only be evaluated one f is called
They are the same?
Some kind of function inside a a list?
9.1.3 Exercise 9.39
## [1] "[[<-" "[[<-.data.frame"
## [3] "[[<-.factor" "[[<-.numeric_version"
## [5] "[[<-.POSIXlt" "[<-"
## [7] "[<-.data.frame" "[<-.Date"
## [9] "[<-.difftime" "[<-.factor"
## [11] "[<-.numeric_version" "[<-.POSIXct"
## [13] "[<-.POSIXlt" "@<-"
## [15] "<-" "<<-"
## [17] "$<-" "$<-.data.frame"
## [19] "$<-.POSIXlt" "attr<-"
## [21] "attributes<-" "body<-"
## [23] "class<-" "colnames<-"
## [25] "comment<-" "diag<-"
## [27] "dim<-" "dimnames<-"
## [29] "dimnames<-.data.frame" "Encoding<-"
## [31] "environment<-" "formals<-"
## [33] "is.na<-" "is.na<-.default"
## [35] "is.na<-.factor" "is.na<-.numeric_version"
## [37] "length<-" "length<-.Date"
## [39] "length<-.difftime" "length<-.factor"
## [41] "length<-.POSIXct" "length<-.POSIXlt"
## [43] "levels<-" "levels<-.factor"
## [45] "mode<-" "mostattributes<-"
## [47] "names<-" "names<-.POSIXlt"
## [49] "oldClass<-" "parent.env<-"
## [51] "regmatches<-" "row.names<-"
## [53] "row.names<-.data.frame" "row.names<-.default"
## [55] "rownames<-" "split<-"
## [57] "split<-.data.frame" "split<-.default"
## [59] "storage.mode<-" "substr<-"
## [61] "substring<-" "units<-"
## [63] "units<-.difftime"
9.1.4 Exercise 9.40
‘Find’ and ‘Position’ give the first or last such element and its position in the vector, respectively.
## [1] 2
## [1] 3
## [1] NA
#Position("notafunction", small_vec)
my_pos <- function(FUN, vec, first = TRUE) {
if (!is.function(FUN)) return('informative error')
if (first) {
idx <- seq_along(vec)
} else idx <- rev(seq_along(vec))
for (i in idx) {
res <- even(vec[[i]])
if (res) {
return(i)}
}
return(NA_integer_)
}
all.equal(my_pos(even, small_vec), Position(even , small_vec))
## [1] TRUE
## [1] TRUE
## [1] TRUE
## [1] "informative error"