如何在R中使用tabulator()重新排序flextable的标题?

huangapple go评论61阅读模式
英文:

How can I reorder a flextable's headers when using tabulator() in R?

问题

这是当前的flextable:

如何在R中使用tabulator()重新排序flextable的标题?

这是通用的示例数据:

如何在R中使用tabulator()重新排序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 SizeRegion 之前(flextable会自动完成),在那之下会是 SmallMediumLarge,然后是 WestEast

我了解到,当我想按更特定的顺序排列时,tabulator() 会自动按字母顺序排列。

英文:

Here is the current flextable:

如何在R中使用tabulator()重新排序flextable的标题?

Here is the general example data:

如何在R中使用tabulator()重新排序flextable的标题?

library(flextable)

dat &lt;- data.frame(question = c(&quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;),
                 column_one = c(&quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Region&quot;, &quot;Region&quot;, &quot;Region&quot;, &quot;Region&quot;),
                 column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
                 column_two = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;West&quot;, &quot;East&quot;, &quot;West&quot;, &quot;East&quot;),
                 column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
                 row_name = c(&quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;),
                 row = c(&quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;, &quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;),
                 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 &lt;- tabulator(
  x = dat,
  rows = &quot;row&quot;,
  columns = c(&quot;question&quot;, &quot;column_one&quot;,&quot;column_two&quot;),
  `P` = as_paragraph(percent),
  `SE` = as_paragraph(standardError)
)

ft &lt;- 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

如何在R中使用tabulator()重新排序flextable的标题?

英文:

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 &lt;- data.frame(
question = c(&quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;, &quot;Food Satisfaction&quot;),
column_one = c(&quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Portion Size&quot;, &quot;Region&quot;, &quot;Region&quot;, &quot;Region&quot;, &quot;Region&quot;),
column_one_order = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2),
column_two = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;West&quot;, &quot;East&quot;, &quot;West&quot;, &quot;East&quot;),
column_two_order = c(1, 2, 3, 1, 2, 3, 1, 2, 1, 2),
row_name = c(&quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;, &quot;Food&quot;),
row = c(&quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;, &quot;Pizza&quot;, &quot;Pizza&quot;, &quot;Hamburger&quot;, &quot;Hamburger&quot;),
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 &lt;- factor(dat$column_one, levels = c(&quot;Portion Size&quot;, &quot;Region&quot;))
dat$column_two &lt;- factor(dat$column_two, levels = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;West&quot;, &quot;East&quot;))
cft &lt;- tabulator(
x = dat,
rows = &quot;row&quot;,
columns = c(&quot;question&quot;, &quot;column_one&quot;, &quot;column_two&quot;),
`P` = as_paragraph(percent),
`SE` = as_paragraph(standardError)
)
ft &lt;- as_flextable(cft)
ft

如何在R中使用tabulator()重新排序flextable的标题?

huangapple
  • 本文由 发表于 2023年2月19日 04:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定