0 votes
1 view
in R Programming by (6.5k points)

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?

1 Answer

0 votes
by (25.3k points)

You can use the str function to figure out the data type of the output data:

str()

From utils v3.6.1

by R-core [email protected]

Compactly Display The Structure Of An Arbitrary R Object

Compactly display the internal structure of an R object, a diagnostic function and an alternative to summary (and to some extent, dput). Ideally, only one line for each ‘basic’ structure is displayed. It is especially well suited to compactly display the (abbreviated) contents of (possibly nested) lists. The idea is to give reasonable output for any R object. It calls args for (non-primitive) function objects.

For example:

d <- rnorm(100)

df <- data.frame(y=d,x=1)

 str(df)

'data.frame': 100 obs. of  2 variables:

 $ y: num  -0.12 1.013 -0.201 -2.038 -0.196 ...

 $ x: num  1 1 1 1 1 1 1 1 1 1 ...

Other functions to determine the type of output:

typeof()

The Type Of An Object

typeof determines the (R internal) type or storage-mode of any object.

class(obj) 

sapply(obj, class) 

sapply(obj, attributes)

attributes(obj) 

names(obj)

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...