在R中使用formattable时打印空白而不是NA

bma*_*TPM 9 r formattable

考虑示例data.frame

df <- data.frame(
  id = 1:4,
  name = c("Bob", "Ashley", "James", "David"), 
  age = c(48, NA, 40, 28),
  test1_score = c(18.9, 19.5, NA, 12.9),
  stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)

我正在使用R包格式化表来创建一个漂亮的表.

library(formattable)
formattable(df, list(
age = color_tile("white", "orange"),
test1_score = color_bar("pink", 'proportion', 0.2)
))
Run Code Online (Sandbox Code Playgroud)

过去,NA会自动不打印,而是打印出一个空白.看起来这不再是默认设置,但我还是想为NA打印一个空白.像这样替换NA有效:

df[is.na(df)]=''
formattable(df, list(
  age = color_tile("white", "orange"),
  test1_score = color_bar("pink", 'proportion', 0.2)
))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是,如果我尝试格式化其中一个列以强制它有2个小数位,那么麻烦的NA会返回:

df$age = digits(df$age, digits=2)
formattable(df, list(
age = color_tile("white", "orange"),
test1_score = color_bar("pink", 'proportion', 0.2)
))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果我再次移除NA,则NA会消失,但小数位也会消失

df[is.na(df)] = ''
formattable(df, list(
age = color_tile("white", "orange"),
test1_score = color_bar("pink", 'proportion', 0.2)
))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我相信原因是数字转换df$ageformattable numeric对象并创建NA,并df[is.na(df)] = ''转换df$ageformattable character对象:

> df$age = digits(df$age, digits=2)
> df$age
[1] 48.00  NA   40.00 28.00
> class(df$age)
[1] "formattable" "numeric"    
> df[is.na(df)] = ''
> df$age
[1] "48" "  " "40" "28"
> class(df$age)
[1] "formattable" "character" 
Run Code Online (Sandbox Code Playgroud)

关于解决方案的任何想法?

最后,我还想在过滤的data.frame中使用它,在这里我使用过滤带有formattable的数据帧的代码来确保在过滤data.frame时颜色标度保持不变:

df$age = digits(df$age, digits=2)
  subset_df <- function(m) {
    formattable(df[m, ], list(
      age = x ~ color_tile("white", "orange")(df$age)[m],
      test1_score = x ~ color_bar("pink", 'proportion', 0.2)(df$test1_score)[m],
      test2_score = x ~ color_bar("pink", 'proportion', 0.2)(df$test2_score)[m]
    ))
  }

subset_df(1:3)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但问题似乎与此代码无关.

eip*_*i10 5

您可以使用该sprintf函数将数字列格式化为具有所需小数位数的字符串。在下面的代码中,sprintf转换NA为 string "NA",然后我们将其转换为空字符串。

# Function to convert numeric values to strings with a given number of 
#  decimal places, and convert NA to empty string
fnc = function(var, decimal.places) {
  var = sprintf(paste0("%1.",decimal.places,"f"), var)
  var[var=="NA"] = ""
  var
}

# Select the columns we want to reformat
vars = c('age', 'test1_score')

# Apply the function to the desired columns with the desired number of decimal places
df[ , vars] = mapply(fnc, df[ ,vars], 2:3)

formattable(df, list(
  age = color_tile("white", "orange"),
  test1_score = color_bar("pink", 'proportion', 0.2)
))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明