Below I have a working example of what I would like the function to do, and then script for the function, noting where the Error occurs.
The error message is:
Error: index out of bounds Which I know usually means R can’t find the variable that’s being called.
Interestingly, in my function example below, if I only group by my subgroup_name (which is passed to the function and becomes a column in the newly created dataframe) the function will successfully regroup that variable, but I also want to group by a newly created column (from the melt) called variable.
Similar code used to work for me using regroup(), but that has been deprecated. I am trying to use group_by_() but to no avail.
I have read many other posts and answers and experimented several hours today but still not successful.
# Initialize example dataset database <- ggplot2::diamonds database$diamond <- row.names(diamonds) # needed for melting subgroup_name <- "cut" # can replace with "color" or "clarity" subgroup_column <- 2 # can replace with 3 for color, 4 for clarity # This works, although it would be preferable not to need separate variables for subgroup_name and subgroup_column number df <- database %>% select(diamond, subgroup_column, x,y,z) %>% melt(id.vars=c("diamond", subgroup_name)) %>% group_by(cut, variable) %>% summarise(value = round(mean(value, na.rm = TRUE),2)) # This does not work, I am expecting the same output as above subgroup_analysis <- function(database,...){ df <- database %>% select(diamond, subgroup_column, x,y,z) %>% melt(id.vars=c("diamond", subgroup_name)) %>% group_by_(subgroup_name, variable) %>% # problem appears to be with finding "variable" summarise(value = round(mean(value, na.rm = TRUE),2)) print(df) } subgroup_analysis(database, subgroup_column, subgroup_name)
...instead of named arguments?group_by_(subgroup_name, quote(variable))printcall at the end and just writedf, or not even assigndfat all in the function. Otherwise the result prints even when you dox <- subgroup_analysis(...)