有线更改行顺序

问题描述

我正在尝试使用kable包在LaTeX中创建一个表。但是,它不会按我想要的顺序打印行。

\documentclass{article}

\usepackage{float} %necessari per a kable
\usepackage{booktabs} %necessari per a kable

\begin{document}

<<>>=
library(kableExtra)
options(kableExtra.latex.load_packages=FALSE)
knit_hooks$set(document = function(x) {sub('\\usepackage[]{color}','\\usepackage[table]{xcolor}',x,fixed=TRUE)})

necessari <- function(diff,escrutat) { 
  -(diff*escrutat)/(1-escrutat)
}

library(tibble)

as_of <- tibble(
  "Estat" = c("Arizona","Geòrgia","Maine","Michigan","Nevada","Carolina del nord","Pensilvània","Wisconsin"),"Avantatge" = c("Biden","Trump","Biden","Trump"),"Diferència (%)" = c(-6.1,2.5,-8.2,8.6,-2.7,1.4,14.4,3.8),"Escrutat (%)" = c(80,91,62,63,78,95,68,78)
)

necessita <- round(necessari(as_of[[3]],as_of[[4]]),2)

as_of <- add_column(as_of,"Necessari (%)" = necessita)

as_of <- arrange(as_of,`Necessari (%)`)

as_of$`Diferència (%)` <- abs(as_of$`Diferència (%)`)
as_of$`Necessari (%)` <- abs(as_of$`Necessari (%)`)

as_of$Estat <- factor(as_of$Estat,levels = as_of$Estat)

taula_as_of <- kable(as_of,"latex",booktabs=T)
taula_as_of <- row_spec(taula_as_of,which(as_of$Avantatge == "Trump"),background = "red")
taula_as_of <- row_spec(taula_as_of,which(as_of$Avantatge == "Biden"),background = "blue")

taula_as_of
@


\end{document}

奇怪的是,taula_as_of显然在该块的末尾有缅因州,但是当我编译PDF时,却得到了Arizona(这就像我没有做任何arrange) 。至于factor,我已经尝试过了,但是根本解决不了。

> taula_as_of

\begin{tabular}{llrrr}
\toprule
Estat & Avantatge & Diferència (\%) & Escrutat (\%) & Necessari (\%)\\
\midrule
\cellcolor{blue}{Maine} & \cellcolor{blue}{Biden} & \cellcolor{blue}{8.2} & \cellcolor{blue}{62} & \cellcolor{blue}{8.33}\\
\cellcolor{blue}{Arizona} & \cellcolor{blue}{Biden} & \cellcolor{blue}{6.1} & \cellcolor{blue}{80} & \cellcolor{blue}{6.18}\\
\cellcolor{blue}{Nevada} & \cellcolor{blue}{Biden} & \cellcolor{blue}{2.7} & \cellcolor{blue}{78} & \cellcolor{blue}{2.74}\\
\cellcolor{red}{Carolina del nord} & \cellcolor{red}{Trump} & \cellcolor{red}{1.4} & \cellcolor{red}{95} & \cellcolor{red}{1.41}\\
\cellcolor{red}{Geòrgia} & \cellcolor{red}{Trump} & \cellcolor{red}{2.5} & \cellcolor{red}{91} & \cellcolor{red}{2.53}\\
\addlinespace
\cellcolor{red}{Wisconsin} & \cellcolor{red}{Trump} & \cellcolor{red}{3.8} & \cellcolor{red}{78} & \cellcolor{red}{3.85}\\
\cellcolor{red}{Michigan} & \cellcolor{red}{Trump} & \cellcolor{red}{8.6} & \cellcolor{red}{63} & \cellcolor{red}{8.74}\\
\cellcolor{red}{Pensilvània} & \cellcolor{red}{Trump} & \cellcolor{red}{14.4} & \cellcolor{red}{68} & \cellcolor{red}{14.61}\\
\bottomrule
\end{tabular} 

Resulting table

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)