如以下 MWE 所示,香蕉数量处的 NA 如何变为空白而不是显示“NA”?我希望数字列像字符列一样工作(请参阅 MWE 中的苹果颜色)。
library(data.table)
library(flextable)
the.data <- data.table(Fruit=c("Apples", "Oranges", "Bananas", "Pears"), Amount=c(4L, 8L, NA_integer_, 2L), Color=c(NA_character_, "Orange", "Yellow", "Green"))
the.ft <- flextable(the.data)
the.ft
Run Code Online (Sandbox Code Playgroud)
一种方法是将数字列转换为字符,但也许有更好的方法。
我将致力于将该案例集成到包中。同时,以下代码可让您为 NA 显示空白。
library(flextable)
the.data <- data.table(
Fruit=c("Apples", "Oranges", "Bananas", "Pears"),
Amount=c(4L, 8L, NA_integer_, 2L),
Color=c(NA_character_, "Orange", "Yellow", "Green"))
the.ft <- regulartable(the.data)
the.ft <- set_formatter(
the.ft,
Amount = function(x) ifelse(is.na(x), "", sprintf("%d", x) ),
Color = function(x) ifelse(is.na(x), "", x )
)
the.ft
Run Code Online (Sandbox Code Playgroud)