英文:
How can I reorder a flextable's headers when using tabulator() in R?
问题
这是当前的flextable:
这是通用的示例数据:
library(flextable)
dat <- data.frame(question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
row_name = c("Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food"),
row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473))
dat
cft <- tabulator(
x = dat,
rows = "row",
columns = c("question", "column_one","column_two"),
`P` = as_paragraph(percent),
`SE` = as_paragraph(standardError)
)
ft <- as_flextable(cft)
ft
有没有一个参数或方法可以根据列 [column_one_order] 和 [column_two_order] 重新排列表头?我希望看到 Portion Size 在 Region 之前(flextable会自动完成),在那之下会是 Small、Medium、Large,然后是 West 和 East。
我了解到,当我想按更特定的顺序排列时,tabulator() 会自动按字母顺序排列。
英文:
Here is the current flextable:
Here is the general example data:
library(flextable)
dat <- data.frame(question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
row_name = c("Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food"),
row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473))
dat
cft <- tabulator(
x = dat,
rows = "row",
columns = c("question", "column_one","column_two"),
`P` = as_paragraph(percent),
`SE` = as_paragraph(standardError)
)
ft <- as_flextable(cft)
ft
Is there a parameter or a method where I can rearrange the header based on columns: [column_one_order] and [column_two_order]? So I would want to see Portion Size before Region (which flextable automatically does) and below that would be Small, Medium, Large and then West and East.
My understanding is that tabulator() is automatically arranging it alphabetically when I would like to have it in a more specific order.
答案1
得分: 1
你可以使用因子(而不是字符,如ggplot2或table)来控制订单,在这种情况下,因子级别被用作顺序,而不是按字母顺序:
library(flextable)
dat <- data.frame(
question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
row_name = c("Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food"),
row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473)
)
dat
dat$column_one <- factor(dat$column_one, levels = c("Portion Size", "Region"))
dat$column_two <- factor(dat$column_two, levels = c("Small", "Medium", "Large", "West", "East"))
cft <- tabulator(
x = dat,
rows = "row",
columns = c("question", "column_one", "column_two"),
`P` = as_paragraph(percent),
`SE` = as_paragraph(standardError)
)
ft <- as_flextable(cft)
ft
英文:
You can control orders by using factor instead of characters (as with ggplot2 or table), in that case factor levels are used as order and not alphabetical order:
library(flextable)
dat <- data.frame(
question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
row_name = c("Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food"),
row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473)
)
dat
dat$column_one <- factor(dat$column_one, levels = c("Portion Size", "Region"))
dat$column_two <- factor(dat$column_two, levels = c("Small", "Medium", "Large", "West", "East"))
cft <- tabulator(
x = dat,
rows = "row",
columns = c("question", "column_one", "column_two"),
`P` = as_paragraph(percent),
`SE` = as_paragraph(standardError)
)
ft <- as_flextable(cft)
ft
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论