微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有线更改行顺序

如何解决有线更改行顺序

我正在尝试使用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 举报,一经查实,本站将立刻删除。