My aim is to join a dataframe to a dataframes held within a nested list-column, eg:
data(mtcars)
library(tidyr)
library(purrr)
mtcars_nest <- mtcars %>% rownames_to_column() %>% rename(rowname_1 = rowname) %>% select(-mpg) %>% group_by(cyl) %>% nest()
mtcars_mpg <- mtcars %>% rownames_to_column() %>% rename(rowname_2 = rowname) %>% select(rowname_2, mpg)
join_df <- function(df_nest, df_other) {
df_all <- df_nest %>% inner_join(df_other, by = c("rowname_1" = "rowname_2"))
}
join_df <- mtcars_nest %>%
mutate(new_mpg = map_df(data, join_df(., mtcars_mpg)))
This returns the following error:
# Error in mutate_impl(.data, dots) : Evaluation error: `by` can't contain join column `rowname_1` which is missing from LHS.
So the dataframe map_*
receives from the nested input isn't offering a column name (ie rowname_1
) to take part in the join. I can't work out why this is the case. I'm passing the data
column that contains dataframes from the nested dataframe.
I want a dataframe output that can be added to a new column in the input nested dataframe, eg
| rowname_1 | cyl | disp |...|mpg|
|:----------|:----|:-----|:--|:--|