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 2

  • Use 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.1 Exercise 9.37

f <- function(x)
    for (e in x)
        print(e)
x <- list(1, 2,c(3, 4))
f(x)
## [1] 1
## [1] 2
## [1] 3 4
f(list(1, NULL, 3))
## [1] 1
## NULL
## [1] 3

9.1.2 Exercise 9.38

The replacement forms return their right hand side.

9.1.3 Exercise 9.39

base_env <- ls(envir = baseenv())
base_env[grep("<-", base_env)]
##  [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"
#sadly we still have "<<-" that should be excluded

9.1.4 Exercise 9.40

‘Find’ and ‘Position’ give the first or last such element and its position in the vector, respectively.

small_vec <- c(1, 2, 2, 3, 5)
even <- function(x) {x %% 2 == 0}
Position(even , small_vec)
## [1] 2
Position(even, small_vec, right = TRUE)
## [1] 3
Position(even, c(1,3,5))
## [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
all.equal(my_pos(even, small_vec, first = FALSE), 
          Position(even, small_vec, right = TRUE))
## [1] TRUE
all.equal(my_pos(even, c(1,3,5)), 
          Position(even, c(1,3,5)))
## [1] TRUE
my_pos("notafunction", small_vec)
## [1] "informative error"