In nested data frame, pass information from one list column to function applied in another

久未见 提交于 2020-01-11 12:33:13

问题


I am working on a report for which I have to export a large number of similar data frames into nice looking tables in Word. My goal is to achieve this in one go, using flextable to generate the tables and purrr / tidyverse to apply all the formatting procedures to all rows in a nested data frame. This is what my data frame looks like:

df <- data.frame(school = c("A", "B", "A", "B", "A", "B"),
            students = c(round(runif(6, 1, 10), 0)),
            grade = c(1, 1, 2, 2, 3, 3))

I want to generate separate tables for all groups in column 'school' and started by using the nest() function within tidyr.

list <- df %>% 
        group_by(school) %>%
        nest()

This gives me a nested data frame to which I can apply the functions in flextable using purrr:

list <- list %>% 
        mutate(ftables = map(data, flextable)) %>%
        mutate(ftables = purrr::map(ftables, ~ set_header_labels(.,
                                               students = "No of students",
                                               grade = "Grade")))

The first mutate generates a new column with flextable objects for each school, and the second mutate applies header labels to the table, based on the column names that are saved in the object.

My goal is now to add another header that is based on the name of the school. This value resides in the list column entitled school, which corresponds row-wise to the tables generated in the list column ftables. How can I pass the name of the school to the add_header function within ftables, using purrr or any other procedure?

Expected output
I have been able to achieve what I want for individual schools with this procedure (identical header cells will later be merged):

school.name <- "A"

ftable.a <- df %>%
            filter(school == "A") %>% 
            select(-school) %>%
            flextable() %>%
            set_header_labels(students = "No of students",
                              grade = "Grade") %>%
            add_header(students = school.name,
                       grade = school.name)

ftable.a


回答1:


package purrr provides function map2 that you should use:

library(flextable)
library(magrittr)
library(dplyr)
library(tidyr)
library(purrr)


df <- data.frame(school = c("A", "B", "A", "B", "A", "B"),
                 students = c(round(runif(6, 1, 10), 0)),
                 grade = c(1, 1, 2, 2, 3, 3))
byschool <- df %>% 
  group_by(school) %>%
  nest()

byschool <- byschool %>% 
  mutate(ftables = map(data, flextable)) %>%
  mutate(ftables = purrr::map(
    ftables, ~ set_header_labels(.,
                                 students = "No of students",
                                 grade = "Grade"))) %>% 
  mutate(ftables = purrr::map2(ftables, school, function(ft, h){
    add_header(ft, students = h, grade = h)
  } )) 


来源:https://stackoverflow.com/questions/48418333/in-nested-data-frame-pass-information-from-one-list-column-to-function-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!