I have been studying is.numeric and as.numeric, is.complex and as.complex, etc. One thing I don't get is the difference between mode() and storage.mode(). Mode() tells me whether it is numeric, complex, logical and char right? then what exactly does storage.mode() do? I don't see why there needs to be storage.mode. Any explanations are very appreciated :)
-
weren't these questions answered in the details in the help files? – rawr Feb 07 '16 at 22:19
-
I still don't get it after reading the ?mode... – 정지수 Feb 07 '16 at 22:54
-
1The best way to see the difference is to look at the source code for both functions. At the command line, type `mode`, press return, and have a look, then type `storage.mode` and see what it does differently. (Both employ `typeof` under the hood.) You might also find [this answer](http://stackoverflow.com/a/8857411/980833) of interest. Its first couple of lines get at the real reason there are so many functions for examining object 'type' in R. – Josh O'Brien Feb 07 '16 at 23:58
1 Answers
Both mode and storage.mode return a character string giving the (storage) mode of the object — often the same — both relying on the output of typeof(x), see the example below.
mode(x) <- "newmode" changes the mode of object x to newmode. This is only supported if there is an appropriate as.newmode function, for example "logical", "integer", "double", "complex", "raw", "character", "list", "expression", "name", "symbol" and "function". Attributes are preserved (but see below).
storage.mode(x) <- "newmode" is a more efficient primitive version of mode<-, which works for "newmode" which is one of the internal types (see typeof), but not for "single". Attributes are preserved.
As storage mode "single" is only a pseudo-mode in R, it will not be reported by mode or storage.mode: use attr(object, "Csingle") to examine this. However, mode<- can be used to set the mode to "single", which sets the real mode to "double" and the "Csingle" attribute to TRUE. Setting any other mode will remove this attribute. -- from R Documents online

- 11
- 2
-
Could you please provide a very simple example(s) which helps explain your goal!?! This is called a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – mccurcio Mar 28 '21 at 15:59