Is there a way to list objects based on their classes ? For example, list only the dataframe objects in the workspace.
Asked
Active
Viewed 2,384 times
2 Answers
3
Here is a way with sapply and class to retrieve the names of the objects in the environment that are data.frames:
ls()[sapply(ls(), function(i) class(get(i))) == "data.frame"]
lmo
- 36,904
- 9
- 50
- 61
-
Why are you complicating this? Use this `ls()[sapply(ls(),function(t) is.data.frame(get(t)))]` – user2100721 Jul 15 '16 at 12:54
-
That would return something different than what I said. That would be a logical vector. I am returning the names of the objects. I'm not sure that your statement is that much cleaner than mine. – lmo Jul 15 '16 at 12:57
-
1The exact code is already there in the dupe post which was dupe tagged 4 mins before this was posted. – akrun Jul 15 '16 at 12:57
-
@user2100721 Sorry for the confusion. It was the other way around. Thanks for asking. To be clear, `ls()[sapply(ls(), function(i) class(get(i))) == "data.frame"]` didn't list the data table objects (which are dataframes too) but `ls()[sapply(ls(),function(t) is.data.frame(get(t)))]` listed all dataframes. – Veera Jul 15 '16 at 13:17
0
for (obj in ls()) {
if(class(get(obj)) == "data.frame")
print(obj)
}
Ronak Shah
- 355,584
- 18
- 123
- 178