Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R object identification

Tags:

object

r

I am often ending up with a function producing output for which I don't understand the output data type. I'm expecting a list and it ends up being a list of lists or a data frame or something else. What's a good method or workflow for figuring out the output data type when first using a function?

like image 954
JD Long Avatar asked Oct 10 '22 21:10

JD Long


People also ask

How do I identify an object in R?

To get type of a value or variable or object in R programming, call typeof() function and pass the value/variable to it.

What does object identification mean?

Object identification in object models means that every object instance has a unique, unchanging identity. Object identification is often referred to as an OID. OIDs are used to reference object instances.

What type of object is R?

R's basic data types are character, numeric, integer, complex, and logical. R's basic data structures include the vector, list, matrix, data frame, and factors.


1 Answers

I usually start out with some combination of:

typeof(obj)
class(obj)
sapply(obj, class)
sapply(obj, attributes)
attributes(obj)
names(obj)

as appropriate based on what's revealed. For example, try with:

obj <- data.frame(a=1:26, b=letters)
obj <- list(a=1:26, b=letters, c=list(d=1:26, e=letters))
data(cars)
obj <- lm(dist ~ speed, data=cars)

..etc.

If obj is an S3 or S4 object, you can also try methods or showMethods, showClass, etc. Patrick Burns' R Inferno has a pretty good section on this (sec #7).

EDIT: Dirk and Hadley mention str(obj) in their answers. It really is much better than any of the above for a quick and even detailed peek into an object.

like image 185
ars Avatar answered Nov 09 '22 23:11

ars