I want to make a table with all the dim() of objects in the environment. ls() returns a list of characters which lapply will not take as object names and operate properly on. What to do? Using R.
> lapply(ls(), dim)
just returns
[[1]]
NULL
I want to make a table with all the dim() of objects in the environment. ls() returns a list of characters which lapply will not take as object names and operate properly on. What to do? Using R.
> lapply(ls(), dim)
just returns
[[1]]
NULL
1) Use eapply
and then optionally use Filter
to remove components with no dim. This creates a named list with the dimensions in the corresponding components.
Filter(length, eapply(.GlobalEnv, dim))
2) A variation would be to create a matrix result such that the row names are the variable names.
do.call("rbind", eapply(.GlobalEnv, dim))
3) or to restrict the output to data frames and not arrays (as arrays might cause problems if there are arrays not of two dimensions) then:
df_dim <- function(x) if (is.data.frame(x)) dim(x)
do.call("rbind", eapply(.GlobalEnv, df_dim))
4) or to restrict it to objects having 2 dimensions including both data frames and arrays:
two_dim <- function(x, dimx = dim(x)) if (length(dimx) == 2) dimx
do.call("rbind", eapply(.GlobalEnv, two_dim))
or
do.call("rbind", Filter(function(x) length(x) == 2, eapply(.GlobalEnv, dim)))
You may use parse and eval to change the string vector to env objects and then use dim.
lapply(ls(), function(x) dim(eval(parse(text=x))))