在嵌套数据框中,将信息从一个列表列传递到另一个列表中应用的函数 [英] In nested data frame, pass information from one list column to function applied in another
问题描述
我正在处理一个报告,必须将大量类似的数据框导出到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))
我想为学校"列中的所有组生成单独的表,并开始使用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
为每个学校生成一个带有可伸缩对象的新列,第二个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
推荐答案
程序包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屋!