I am writing a function that can group and concatenate variables using the dplyr package:
basket<-function(dataframe, group, target)
{
dataframe %>%
group_by_(group) %>%
summarise(new_target=paste(as.character(target), collapse="_"))
}
I am using the mtcars dataset for testing:
basket(mtcars, mtcars$am, mtcars$wt)
The desired output should be something like this:
am wt
0 2.62_2.875_2.32...
1 3.215_3.19_3.44...
However, in my code the group_by_ function fails to successfully create groups based on "am". The result I get is simply a concatenated string of all values of "wt":
[1] "2.62_2.875_2.32_3.215_3.44_3.46_3.57_3.19_3.15_3.44_3.44_4.07_3.73_3.78...
If I use group_by then I'll receive this error:
stop(structure(list(message = "unknown variable to group by : group",
call = resolve_vars(new_groups, tbl_vars(.data)), cppstack = structure(list(
file = "", line = -1L, stack = "C++ stack not available on this system"), .Names = c("file",
"line", "stack"), class = "Rcpp_stack_trace")), .Names = c("message", ...
Has anybody seen this problem before?