You could retrieve ls()
and check the class of everything. It may not be particularly efficient though, as it does the filtering after ls()
and not within.
# populate global environment with some vars.
rm(list=ls())
a <- 1
b <- 2
c <- 'foo'
d <- 'asdf'
lst <- ls()
# return everything 'numeric':
lst[sapply(lst,function(var) any(class(get(var))=='numeric'))]
# 'a' 'b'
The get(var)
gets the variable corresponding to the string in var
, so if var
is "a"
then get(var)
retrieves 1 (being the value of variable a
).
As @VincentZoonekynd notes below - it is possible for objects to have multiple classes. Soo class(some_xts_object)
is c("xts","zoo")
-- the above method will return some_xts_object
if you search for xts
objects, but also if you search for zoo
objects.