I am trying to write a function that makes a list of dataframes that contain a certain string.
If I write:
objlist <- ls(pattern = "*_results*")
all_dfs <- ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))]
dfs_list <- mget(Reduce(intersect, list(objlist, all_dfs)))
"dfs_list" contains all the dataframes in the environment that contain "_results" (in my particular case it creates a list of 20 dataframes).
However if I try to convert it to a function:
list_dataframes <- function(pattern) {
objlist <- ls(pattern = pattern)
all_dfs <- ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))]
dfs_list <- mget(Reduce(intersect, list(objlist, all_dfs)))
return(dfs_list)
}
flarp <- list_dataframes("*_results*")
The object "flarp" is a list of 0 elements.
What am I doing wrong here?