小编bma*_*TPM的帖子

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

考虑示例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 …
Run Code Online (Sandbox Code Playgroud)

r formattable

9
推荐指数
1
解决办法
1335
查看次数

标签 统计

formattable ×1

r ×1