Matyas
Matyas

Reputation: 654

data viewing in R language

I am probably an idiot, but I cannot find in the docs how to display objects. A package I installed returns an object called a.

How can I figure out what is in it? There are some matrices and numbers inside this object that I need.

(I admit a year ago (last time I had to use R) I had the same problem, and I found a solution after googling for an hour. This time I lost patience after 20 minutes and I hope someone takes pity on me.)

Upvotes: 0

Views: 150

Answers (3)

Spacedman
Spacedman

Reputation: 94182

The real solution is to read the package documentation. For example, to get the fitted values out of a GLM, you do fitted(a). To get the nearest neighbour distances with splancs:nndistG you get a$dists.

If the return value of a function in a package isn't documented, tell the maintainer. This is a bug.

If you go digging around in the structure of an object, thinking that a$foo is what you want with no documentation, then there's a chance you won't be getting what you think you are getting. For example suppose a model fitting function has a $resid component. You don't know what kind of residuals these are.

Also, there's no guarantee that an upgrade of the package will keep the same definition of $resid, and the change might not be documented because the author wasn't expecting people to dig around in the guts of the objects.

Upvotes: 3

John
John

Reputation: 23758

You could type

a

Or

str(a)

Or

summary(a)

Those are good starts

Upvotes: 8

IRTFM
IRTFM

Reputation: 263342

The str() function is good at disclosing the general structure of an object. You may need to learn how some of the types of objects get displayed. A matrix will not say "matrix' but with rather be displayed with name[rows, cols]

> str(matrix(NA, 4,4) )
 logi [1:4, 1:4] NA NA NA NA NA NA ...

There are various versions of a describe function that are improvements for dataframes over the built-in summary functions. Then there are functions that can be used to determine length, class, mode, and other features.

Upvotes: 7

Related Questions