繁体   English   中英

R的Huxtable包:使用set_background_color()时遇到问题

[英]Huxtable package for R: Problems with using set_background_color()

我尝试为数据框(df_data)中的某些单元着色,如下所示: DATAFRAME_with_highlights

Huxtable简介的启发,我尝试了以下操作:

library(huxtable)
as_hux(df_table)                                                  %>%
 set_background_color(where(df_table["choice_mean"] < 2), 'red')  %>%
 set_background_color(where(df_table["N"] > 110), 'yellow')   

上面的命令在正确的行中为单元格上色–但仅在第一列而不是所需的列(N,choice_mean)/相应的单元格中:

HUXTABLE_with_highlights

非常感谢您的简短答复和帮助!

问题是where(DF_table["choice_mean"] < 2)

这是正在发生的事情,其中​​包含一些数据,以便其他人可以重现该问题(提示):

DF_table <- data.frame(choice_mean = c(1,2,3,1,3), N = c(100, 120, 100, 90, 100))
where(DF_table["choice_mean"] < 2)
##     row col
## [1,]   1   1
## [2,]   4   1

您仅将数据帧的一部分传递到where ,它正确地告诉您,在数据帧的那(1列)部分中,第1行第1行和第4行第1行小于2。然后得出错误的信息给set_background_color

您可以使用标准R子集来解决此问题:

DF_table <- as_hux(DF_table)
set_background_color(DF_table, DF_table$choice_mean < 2, 'choice_mean', 'red')
set_background_color(DF_table, DF_table$N > 110, 'N', 'yellow')

在此, DF_table$N > 110指定行, 'N'列。

这与普通的R子集相同:

DF_table[DF_table$N > 110, 'N']
## 120

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM