'Is there a way to list out all the object classes in R
Is there a way to list the classes of all objects in my environment? I tried
asd <- iris
sapply(ls(), function(x) class(x))
## asd
## "character"
but what I want is
## asd
## "data.frame"
I am assigning the iris data set to the name asd and want its class, but my code is returning "character" instead of "data.frame".
When we have N objects in our environment, we should see how many objects there are and their classes.
Solution 1:[1]
You are looking for:
lapply(mget(ls()), class)
Solution 2:[2]
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, ....
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sweepy Dodo |
| Solution 2 |
