如何在R中为每个表格列添加颜色刻度时修复“条件长度大于1”的错误?

huangapple go评论64阅读模式
英文:

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 &lt;- all_stats &lt;- data.frame(&quot;SEASON&quot; = c(&quot;2022-23&quot;, &quot;2021-22&quot;, &quot;2020-21&quot;, &quot;2019-20&quot;,
                                     &quot;2018-19&quot;, &quot;2017-18&quot;, &quot;2016-17&quot;, &quot;2015-16&quot;),
                        &quot;Cut&quot; = c(1.26,1.31,1.26,1.59,1.39,1.18,2.28,1.00),
                        &quot;Handoff&quot; = c(1.04,1.09,0.77,0.90,0.97,1.05,1.28,0.82),
                        &quot;Isolation&quot; = c(0.97,0.60,0.59,0.92,0.76,0.56,2.04,0.75),
                        &quot;Misc&quot; = c(0.94,0.91,0.88,0.91,0.74,0.92,2.14,0.65),
                        &quot;Off Screens&quot; = c( 1.17,1.08,1.04,0.98,1.08,1.05,2.01,0.82),
                        &quot;PnR Ball Handler&quot; = c(0.88,1.10,1.06,0.81,0.99,0.92,1.89,0.76),
                        &quot;PnR Roll Man&quot; = c(1.26,0.86,0.93,1.36,0.62,1.12,NA,NA))
plays &lt;-c(&quot;Cut&quot;, &quot;Handoff&quot;, &quot;Isolation&quot;, &quot;Misc&quot;, &quot;Off Screens&quot;, &quot;PnR Ball-Handler&quot;,
          &quot;PnR Roll Man&quot;, &quot;Postup&quot;, &quot;Putbacks&quot;, &quot;Spotup&quot;, &quot;Transition&quot;)
t &lt;-  all_stats %&gt;%
    gt() %&gt;%
    tab_header(title = paste0(playerName,&quot; Career Outlook&quot;)) %&gt;%
    cols_label(SEASON = &quot;Season&quot;) %&gt;%
    if((&quot;Cut&quot; %in% plays)){
      data_color(columns = Cut, apply_to = &quot;fill&quot;,
                 colors = col_numeric(domain = NULL,
                                      palette = c(&#39;#CF124B&#39;,&#39;#FFFFFF&#39;,&#39;#2C5F0D&#39;)))

The Error I am getting is:

Error in if (.) (&quot;Cut&quot; %in% plays) else { : the condition has length &gt; 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;%
  gt() %&gt;%
  cols_label(SEASON = &quot;Season&quot;) %&gt;%
  data_color(columns = any_of(&quot;Cut&quot;),
             apply_to = &quot;fill&quot;,
             colors = col_numeric(domain = NULL,
                                  palette = c(&#39;#CF124B&#39;,&#39;#FFFFFF&#39;,&#39;#2C5F0D&#39;)))

If the column exists, it will be included in the colouring. If it doesn't exist, it will be skipped over without an error.

如何在R中为每个表格列添加颜色刻度时修复“条件长度大于1”的错误?

huangapple
  • 本文由 发表于 2023年5月28日 20:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定