使用purrr :: walk从嵌套数据框写入动态文件名

时间:2018-05-02 16:30:39

标签: r tidyr purrr

我正在使用purrr :: map将函数应用于嵌套数据框以获取新的数据帧列表列。

现在我想使用来自同一行的列值作为文件名的一部分,将每个新数据帧写入文件。

我坚持如何拉出其他列值以传递到文件名以写入文件。我有信心purrr::walk应该参与,但是如何访问列变量列表数据帧内容的方式就是问题所在。

下面的代表:

library(tibble) 
library(dplyr)
library(tidyr)  
library(purrr)

# Data
data("mtcars")
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)

# Function to apply to nested dataframe
join_df <- function(df_nest, df_other) {
  df_all <- inner_join(df_nest, df_other, by = c("rowname_1" = "rowname_2"))
  return(df_all)
}

# 1. Apply function to `$data` to get new dataframe list column and add an extra 'case' column for filename
mtcars_nest %>%
  mutate(case = c("first", "second", "third")) %>%
  mutate(new_mpg = map(data, ~ join_df(., mtcars_mpg)))

# 2. Now write `$new_mpg` to file with filename sources from $cyl and $case
# I think `walk` is the correct to use but how to pass the two row values into filename?

## Not real code##
# mtcars_nest %>%
#  walk(., function(x) {write.csv(., file = paste0(cyl, "_", case, ".csv")})

1 个答案:

答案 0 :(得分:3)

使用pwalk

... %>%
select(cyl, case, new_mpg) %>%
pwalk(~ write.csv(..3, file = paste0(..1, '_', ..2, '.csv')))

代码链后:

mtcars_nest %>%
    mutate(case = c("first", "second", "third")) %>%
    mutate(new_mpg = map(data, ~ join_df(., mtcars_mpg))) %>%
    select(cyl, case, new_mpg) %>%
    pwalk(~ write.csv(..3, file = paste0(..1, '_', ..2, '.csv')))