2

My research so far

Most articles discuss the difference between class and typeof but I haven't found much to the differences between mode, storage.mode, and typeof.

I get that mode, storage.mode, and typeof are more similar and class is more different. I get that the former two call typeof and I understand that they handle specific types slightly differently (I even know which ones and how). What I would like to know is what the idea behind that is.

Minimal working example

library(data.table)

my_vector_boolean <- c(TRUE, FALSE, T, F)
my_vector_integer <- c(1L, 2L, 3L)
my_vector_character <- c("a", "b", "abc")
my_vector_factor <- as.factor(c("a", "b", "c"))
my_list <- list(a=1, b=2L, c="c")
my_matrix <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE,
                    dimnames = list(c("row1", "row2"), c("col1", "col2", "col3")))
my_dataframe <- data.frame(1, 1L:10L, sample(3, 10, replace = TRUE))
my_datatable <- data.table(1, 1L:10L, sample(3, 10, replace = TRUE))

datatypeslist <- list(
  my_vector_boolean = my_vector_boolean,
  my_vector_integer = my_vector_integer,
  my_vector_character = my_vector_character,
  my_vector_factor = my_vector_factor,
  my_list = my_list,
  my_matrix = my_matrix,
  my_dataframe = my_dataframe,
  my_datatable = my_datatable
  )

multi.sapply <- function(...) {
      arglist <- match.call(expand.dots = FALSE)$...
      var.names <- sapply(arglist, deparse)
      has.name <- (names(arglist) != "")
      var.names[has.name] <- names(arglist)[has.name]
      arglist <- lapply(arglist, eval.parent, n = 2)
      x <- arglist[[1]]
      arglist[[1]] <- NULL
      result <- sapply(arglist, function (FUN, x) sapply(x, FUN), x)
      colnames(result) <- var.names[-1]
      return(result)
}

multi.sapply(datatypeslist, typeof, storage.mode, mode, class)

What I am looking for

What kind of answer I am not looking for: "mode and storage.mode handle single differently.

What kind of answer I am loking for: "typeof is the data type that is used in the underlying C implementation, while mode is the data type that ... and storage.mode is the data type that ..."

Additionally I would appreciate a differentiation of the terms "mode" and "type" that have been used in manuals and tutorials. For example it has been said that typeof return the mode of a data object - which seems confusing to me.


I hope that question was specific/clear enough. Please tell me what was not clear, before down-voting or voting for closing.

Make42
  • 12,236
  • 24
  • 79
  • 155
  • Well, [Hadley says this](http://adv-r.had.co.nz/OO-essentials.html): "You may have heard of `mode()` and `storage.mode()`. I recommend ignoring these functions because they’re just aliases of the names returned by `typeof()`, **and exist solely for S compatibility**." If I understand the last bit of your post correctly, this is approaching the kind of answer you want, right? Edit: Also, maybe the R documentation helps: [here](http://stat.ethz.ch/R-manual/R-devel/doc/manual/R-lang.html#Objects). – slamballais Apr 22 '16 at 13:13
  • @Laterow: Yes, I think that is helping a lot. The R documentation you linked too has been read by me, but hasn't answered by question. The link to Hadley is more useful. If you like you can make an answer from this. – Make42 Apr 22 '16 at 13:26
  • @Laterow: Also, maybe you can explain the difference of the terms "type" and "mode" in tutorials (see the question). – Make42 Apr 22 '16 at 13:32
  • I think it's very safe to say that G. Grothendieck knows significantly more about this kind of stuff than I do. You'd probably get much better answers from him :) @Make42 – slamballais Apr 22 '16 at 13:39

1 Answers1

4

typeof is the key function.

The others are variations of typeof that exist solely for the purpose of S compatibilty and are mostly not used today.

storage.mode can be used in S when call calling .Fortran or .C with numeric data so you know whether an input is an integer or double, say. This distinction can often be blurred in R but when passing objects to these languages the distinction is important. It is like typeof in this respect. mode just gives numeric for both of these so it is coarser.

> mode(1L)
[1] "numeric"
> storage.mode(1L)
[1] "integer"

> mode(1)
[1] "numeric"
> storage.mode(1)
[1] "double"

Their operation is best understood by looking at their source. In both cases the source is pretty short.

For example, storage,mode returns the same value as typeof unless the value returned by typeof is "closure", "builtin" or "special" and in those cases returns "function".

storage.mode <- function (x) 
   switch(tx <- typeof(x), closure = , builtin = , special = "function", tx)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • I appreciate the effort but that is exactly what I already wrote in my question and exactly the kind of answer I was not looking for - also - as already stated in my question. – Make42 Apr 22 '16 at 13:28
  • Have clarified how they are used in S. – G. Grothendieck Apr 22 '16 at 13:39
  • Can you add some words to the usage of the terms "mode" and "type" or is this just something tutorials use interchangeably without having put much thought into the exact use of the terminology? – Make42 Apr 22 '16 at 14:45
  • I don't think mode really has an intended meaning in R. It relates to S. Just ignore it. – G. Grothendieck Apr 22 '16 at 14:56
  • Might be helpful to point out the `mode<-` and `storage.mode<-` usages, which have no analogue in `typeof`... Almost all instances of `storage.mode` I see in the R source (cloned from GitHub) are akin to `storage.mode(x) = 'double'` – MichaelChirico Dec 09 '18 at 13:59