John Smith
John Smith

Reputation: 2886

R: Passing a dataframe to a function Dynamically

I'm trying to dynamically pass some data frames into a function which will return some statistical results for each of the data frames. The aim being to graph the statistical results side by side. I am having problems in that my assignment of the data frame to the function results in the name of the dataframe being passed instead of the actual dataframe itself.

SO for example my dataframe is called construct 1 but instead of passing the actual dataframe that represents this construct, the code seems to pass the name construct 1

Can anyone see how to fix this

# Get a list of all the dataframes in the workspace
my.dfs <- names(which(sapply(.GlobalEnv, is.data.frame))) 

# Filter to just the Alpha DataFrames
my.dfs <- as.data.frame(my.dfs) %>%
  filter(grepl('Alpha_', my.dfs)) %>% 
  mutate(my.table = as.character(my.dfs))

# Loop Through Each Table and get the Alpha Statistics
for (i in 1:nrow(my.dfs)){

  construct = my.dfs$my.table[i]
  construct_name = as.character(my.dfs$my.table[i])

  create_cronbach_summ(construct, construct_name)

}

The end result will be something like

Construct Name
Statistic 1
Statistic 2
Statistic 3....

The error message i recieve is:

 Error in UseMethod("select_") : 
  no applicable method for 'select_' applied to an object of class "character

Upvotes: 0

Views: 746

Answers (1)

Eric Lecoutre
Eric Lecoutre

Reputation: 1481

Have a look at deparse(susbtitute(x)) to eval the name and pass the real data.frame for further manipulations. Highly recommended to build a function. By the way, you should consider using lists, having one data.frame / one outcome per level and extracting information out if sapply(mylist,FUN=function(element){element$part}, for example, or sapply(mylist,FUN=function(element){create_cronback_summ(element)}.

Upvotes: 1

Related Questions