Wishlist-for-R
Wishlist-for-R copied to clipboard
WISH: a single way to know what type of an object is…
typeof(x) sometimes returns the type of the object, sometimes the type of an element of the object (if x is a vector, typeof(x) returns the type of each single element of the vector, and not the fact that x is a vector).
It would be nice to have a unified way of knowing the type of an object. Right now, (to the best of my knowledge), we have to try exhaustively a number of functions to get the type of an object.
Can you clarift what you mean by "type of an object" here? R does not have scalars so the "type" of an atomic vector and it's elements are the same...
On Sat, Mar 31, 2018, 11:42 AM Nelle Varoquaux [email protected] wrote:
typeof(x) sometimes returns the type of the object, sometimes the type of an element of the object (if x is a vector, typeof(x) returns the type of each single element of the vector, and not the fact that x is a vector).
It would be nice to have a unified way of knowing the type of an object. Right now, (to the best of my knowledge), we have to try exhaustively a number of functions to get the type of an object.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/HenrikBengtsson/Wishlist-for-R/issues/66, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3dsUYj3Er7Ih4IzKZV7BLBMUEzXUynks5tj83ggaJpZM4TCkSO .
I concur with NellV wish. Indeed, it would be nice to have harmonized/consistent object introspection functions (Python is nicely design in that respect). In R, we have typeof(), class() and str() which are somewhat redundant and somewhat conflicting. For example, typeof(my_data.frame) returns "list" and not "data.frame", while both class(my_data.frame) and str(my_data.frame) return "tbl_df", "tbl" and "data.frame" but not "list". As an additional issue, objects have no proper name (their sub-parts possibly do), but functions do return their "non-name", like quote() or substitute(), although the returned object is not a string.
So there are a few things going on here.
First off, type and class are (substantially) different concepts in R. S3 class is a label used for method dispatch, and thats it. It does not dictate the contents or structure of an object at all (there are some exceptions for the built in atomic classes, I think, but thats only really relevant as an aside here).
I can do
> x = 1:10
> class(x) = "data.frame"
> class(x)
[1] "data.frame"
typeof() does something else entirely. It reflects the internal SEXP type of the object regardless of what class it has been labeled with.
As for typeof(), it always returns the type of the whole object (or vector if it is a vector). "integer" is a vector type in R. R does not (at the R level) have scalars, only vectors of length one. so typeof is consistent in that regard.
Now S4 classes do define specific mandatory structure, which muddies the water a bit, but I don't get the feeling that thats what we're talkinga bout here.
A data.frame (the tibble or original version) is stored internally by R as a list. So typeof is correct to say its type is list. the output of typeof never has any bearing on method dispatch though (the only time it will even match is when typeof and class are the same, and even then the class is what method dispatch is using).
Hope that helps
So there are a few things going on here.
First off, type and class are (substantially) different concepts in R. S3 class is a label used for method dispatch, and thats it. It does not dictate the contents or structure of an object at all (there are some exceptions for the built in atomic classes, I think, but thats only really relevant as an aside here).
I can do
> x = 1:10 > class(x) = "data.frame" > class(x) [1] "data.frame"
typeof() does something else entirely. It reflects the internal SEXP type of the object regardless of what class it has been labeled with.
As for typeof(), it always returns the type of the whole object (or vector if it is a vector). "integer" is a vector type in R. R does not (at the R level) have scalars, only vectors of length one. so typeof is consistent in that regard.
Now S4 classes do define specific mandatory structure, which muddies the water a bit, but I don't get the feeling that thats what we're talkinga bout here.
A data.frame (the tibble or original version) is stored internally by R as a list. So typeof is correct to say its type is list. the output of typeof never has any bearing on method dispatch though (the only time it will even match is when typeof and class are the same, and even then the class is what method dispatch is using).
Hope that helps
Yes it does, thanks !!
However :
- your above explanations should make it to help guides/manual
- for end-users, like me (and, I believe, the vast majority of R users), the S3/S4 concept is obscure ; any accessible help in that respect would be appreciated
- what end-users care about is what they can do or can't do with R objects, hence they would appreciate to have a function that tells them this information, and help them understand easily warnings and errors.
The goal here is to make R as accessible as possible. It has the reputation of being a difficult language, compared with its proprietary competitors (SAS, SPSS, Stata), and Python is somewhat easier to fathom (again, as an end-user), but of course does not offer such a richness in statistical methods. I think that all efforts to have end-users in mind should be undertaken.