I like the bind_rows
function in dplyr
but I find it annoying that when passing the .id
argument it can only add a numeric index in the new column.
I'm trying write a bind_rows_named
function but am getting stuck accessing the object names. This works as expected:
bind_name_to_df <- function(df){
dfname <- deparse(substitute(df))
df %>% mutate(label=dfname)
}
a <- data_frame(stuff=1:10)
bind_name_to_df(a)
But I can't work out how to apply this to a list of data frames, e.g. using .dots. I want this to work, but I know I have the semantics for the ...
wrong somehow. Can anyone shed light?
b <- data_frame(stuff=1:10)
bind_rows_named <- function(...){
return(
bind_rows(lapply(..., bind_name_to_df)))
}
bind_rows_named(a, b)