Count number of objects
Details
Classes in R can roughly be distinguished as following:
Vector-style classes have the property that
length()
represents number of elements (e.g.,factor
,list
, etc.).Record-style (or dataframe or scalar) classes, on the other hand, have complex structures to represent a single thing (e.g.,
data.frame
,R6
,lm
, etc.).
This function counts objects differently depending on the entered class:
If the argument is a vector or a vector-style class, it will return the
output from length()
function. Otherwise, it will returns 1
.
For example,
length(mtcars)
returns11
, butobjectCount(mtcars)
will return1
, whilelength(list(1, 2))
returns2
, andobjectCount(list(1, 2))
will return2
as well.