如何解决将子集中的当前值与全套中的百分比等级进行比较
我已经为数据集计算了第90个百分位值(按列)。过滤数据以获得一个子集后,我需要根据当前值传递要评估的百分位数值。我在下面提供了完整的代码,并提供了一些解释,以防万一我出错了。
我为R中的格式表创建了一个自定义函数,该函数将color_tile与icontext组合在一起,以在高于特定阈值的值旁边放置一个星形。 color_tiles是基于子集数据的,而星星应根据第90个百分位值进行评估。
library(tidyverse)
library(formattable)
set.seed(123)
df <- tibble( group = sample(letters[1:3],100,replace=TRUE),val1 = sample((1:100),val2 = sample((1000:10000),val3 = runif(100))
# calculate 90th percentile for each column from the full dataset
quant <- df %>%
summarise(across(where(is.numeric),~quantile(.x,.90)))
# create a function to combine color_tile and icontext star
# [!] how do you change the x > 8938 to reference the appropriate quant value? [!]
star_formatter <- function(x,c1="white",c2="white"){
formatter("span",style = x ~ style(display = "block",padding = "0 4px",`border-radius` = "4px",font.weight = "bold",`background-color` = csscolor(gradient(as.numeric(x),c1,c2))),x ~ icontext(ifelse(x > 8938,"star",""),x) # <---- THIS !!!!!
)
}
# create subgroup and formattable
df %>% filter(group == "a") %>%
formattable(list(val1 = star_formatter(c1="white",c2="orange"),# star needs to be scaled for these values
val2 = star_formatter(c1="white",c2="pink"),# star works as an example
val3 = star_formatter(c1="white",c2="lightblue") # star needs to be scaled for these values
)
)
解决方法
修改了自定义函数以获取一个变量,我可以从中传递适当的量化值,例如quant $ val1
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" integrity="sha256-ZsWP0vT+akWmvEMkNYgZrPHKU9Ke8nYBPC3dqONp1mY=" crossorigin="anonymous"></script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。