英文:
How can I fix the 'the condition has length >1' error when adding color scale to each table column in R?
问题
这是我的当前代码:
all_stats <- data.frame("SEASON" = c("2022-23", "2021-22", "2020-21", "2019-20",
"2018-19", "2017-18", "2016-17", "2015-16"),
"Cut" = c(1.26, 1.31, 1.26, 1.59, 1.39, 1.18, 2.28, 1.00),
"Handoff" = c(1.04, 1.09, 0.77, 0.90, 0.97, 1.05, 1.28, 0.82),
"Isolation" = c(0.97, 0.60, 0.59, 0.92, 0.76, 0.56, 2.04, 0.75),
"Misc" = c(0.94, 0.91, 0.88, 0.91, 0.74, 0.92, 2.14, 0.65),
"Off Screens" = c(1.17, 1.08, 1.04, 0.98, 1.08, 1.05, 2.01, 0.82),
"PnR Ball Handler" = c(0.88, 1.10, 1.06, 0.81, 0.99, 0.92, 1.89, 0.76),
"PnR Roll Man" = c(1.26, 0.86, 0.93, 1.36, 0.62, 1.12, NA, NA))
plays <- c("Cut", "Handoff", "Isolation", "Misc", "Off Screens", "PnR Ball-Handler",
"PnR Roll Man", "Postup", "Putbacks", "Spotup", "Transition")
t <- all_stats %>%
gt() %>%
tab_header(title = paste0(playerName, "职业前景")) %>%
cols_label(SEASON = "赛季") %>%
data_color(columns = Cut, apply_to = "fill",
colors = col_numeric(domain = NULL,
palette = c('#CF124B', '#FFFFFF', '#2C5F0D')))
我得到的错误是:
错误信息:条件长度大于 1。
英文:
I'm trying to write code for a table that adds the color scale to each column based on whether that column is in the table. There are 11 possible columns that won't be in each table iteration. I've been trying to use an IF()
function, but I keep on getting the error "the condition has length >1." What am I doing wrong?
Here is my code right now
all_stats <- all_stats <- data.frame("SEASON" = c("2022-23", "2021-22", "2020-21", "2019-20",
"2018-19", "2017-18", "2016-17", "2015-16"),
"Cut" = c(1.26,1.31,1.26,1.59,1.39,1.18,2.28,1.00),
"Handoff" = c(1.04,1.09,0.77,0.90,0.97,1.05,1.28,0.82),
"Isolation" = c(0.97,0.60,0.59,0.92,0.76,0.56,2.04,0.75),
"Misc" = c(0.94,0.91,0.88,0.91,0.74,0.92,2.14,0.65),
"Off Screens" = c( 1.17,1.08,1.04,0.98,1.08,1.05,2.01,0.82),
"PnR Ball Handler" = c(0.88,1.10,1.06,0.81,0.99,0.92,1.89,0.76),
"PnR Roll Man" = c(1.26,0.86,0.93,1.36,0.62,1.12,NA,NA))
plays <-c("Cut", "Handoff", "Isolation", "Misc", "Off Screens", "PnR Ball-Handler",
"PnR Roll Man", "Postup", "Putbacks", "Spotup", "Transition")
t <- all_stats %>%
gt() %>%
tab_header(title = paste0(playerName," Career Outlook")) %>%
cols_label(SEASON = "Season") %>%
if(("Cut" %in% plays)){
data_color(columns = Cut, apply_to = "fill",
colors = col_numeric(domain = NULL,
palette = c('#CF124B','#FFFFFF','#2C5F0D')))
The Error I am getting is:
Error in if (.) ("Cut" %in% plays) else { : the condition has length > 1
答案1
得分: 0
尝试使用 {dplyr}
中的 any_of()
函数:
all_stats %>%
gt() %>%
cols_label(SEASON = "Season") %>%
data_color(columns = any_of("Cut"),
apply_to = "fill",
colors = col_numeric(domain = NULL,
palette = c('#CF124B','#FFFFFF','#2C5F0D')))
如果列存在,则将其包含在颜色设置中。如果不存在,则将跳过而不会报错。
英文:
Try using any_of()
from {dplyr}:
all_stats %>%
gt() %>%
cols_label(SEASON = "Season") %>%
data_color(columns = any_of("Cut"),
apply_to = "fill",
colors = col_numeric(domain = NULL,
palette = c('#CF124B','#FFFFFF','#2C5F0D')))
If the column exists, it will be included in the colouring. If it doesn't exist, it will be skipped over without an error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论