在嵌套数据框中,将信息从一个列表列传递到另一个应用程序中的函数 [英] In nested data frame, pass information from one list column to function applied in another

查看:20
本文介绍了在嵌套数据框中,将信息从一个列表列传递到另一个应用程序中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一份报告,我必须将大量相似的数据框导出到 Word 中漂亮的表格中.我的目标是一次性实现这一目标,使用 flextable 生成表格并使用 purrr/tidyverse 将所有格式化程序应用于所有行在嵌套数据框中.这是我的数据框的样子:

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))

我想为列 'school' 中的所有组生成单独的表,并使用 tidyr 中的 nest() 函数开始.

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()

这给了我一个嵌套的数据框,我可以使用 purrr 应用 flextable 中的函数:

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")))

第一个 mutate 为每个学校生成一个带有 flextable 对象的新列,第二个 mutate 根据保存的列名将标题标签应用于表在对象中.

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.

我现在的目标是添加另一个基于学校名称的标题.该值位于名为 school 的列表列中,它与在列表列 ftables 中生成的表逐行对应.如何使用 purrr 或任何其他程序将学校名称传递给 ftables 中的 add_header 函数?

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

推荐答案

package purrr 提供了你应该使用的函数 map2:

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)
  } )) 

这篇关于在嵌套数据框中,将信息从一个列表列传递到另一个应用程序中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆