How do I get vector of data frame names available in current environment? I've tried:
sapply(ls(), is.data.frame)
But this fails because ls
returns vector of strings. I'm planning to use this list as a input for dynamic selection in Shiny app.
A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.
Accessing the columns of a data frame The column items in a data frame in R can be accessed using: Single brackets [] , which would display them as a column. Double brackets [[]] , which would display them as a list. Dollar symbol $ , which would display them as a list.
One simple approach to creating an empty DataFrame in the R programming language is by using data. frame() method without any params. This creates an R DataFrame without rows and columns (0 rows and 0 columns).
You can use eapply
to loop through the objects in an environment:
x <- 1:10
y <- mtcars
eapply(.GlobalEnv,is.data.frame)
$x
[1] FALSE
$y
[1] TRUE
names(which(unlist(eapply(.GlobalEnv,is.data.frame))))
[1] "y"
I think you're asking for the actual names of these data frames rather than the data frames themselves? You can do:
l <- ls()
l[sapply(l, function(x) is.data.frame(get(x)))]
get()
will return the value of an object given a character name.
Tidier way of doing basically the same thing:
Filter(function(x) is.data.frame(get(x)), ls())
You need get
, try this:
x <- sapply(sapply(ls(), get), is.data.frame)
names(x)[(x==TRUE)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With