Home > other >  Is there a way to list out all the object classes in R
Is there a way to list out all the object classes in R

Time:02-01

Is there a way to list out all the object classes in R. For example,

rm(list = ls())
asd <- iris
sapply(ls(), function(x){class(x)})
        asd 
"character" 
Expected output
        asd 
"data.frame" 

Ideally I am loading iris dataser to asd. So I need to its class. It is returning as "character" instead of data.frame.

When we have N number of objects, we should see how many objects are there and its classes

CodePudding user response:

You are looking for:

lapply(mget(ls()), class)

CodePudding user response:

There is no need for mget(ls()). Use eapply to obtain the result directly:

x <- double()
m <- matrix()
l <- list()
cc <- eapply(environment(), class)
cc
$x
[1] "numeric"

$l
[1] "list"

$m
[1] "matrix" "array" 

If you want a data frame, then you can do:

data.frame(name = names(cc), class = I(unname(cc)))
  name        class
1    x      numeric
2    l         list
3    m matrix, ....
  •  Tags:  
  • Related