Print spaces instead of NA when using formatted in R

Consider the example 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) 

I use R package, able to make a beautiful table.

 library(formattable) formattable(df, list( age = color_tile("white", "orange"), test1_score = color_bar("pink", 'proportion', 0.2) )) 

It used to be that NA was not automatically printed, and instead a space was printed. It doesn't seem to be the default anymore, but I would still like to print a space for NA. Replacing NA, how it works:

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

enter image description here

However, if I try to format one of the columns to make it have 2 decimal places, then the annoying NA return:

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

enter image description here

If I delete NA again, NA will disappear, but so will the decimal places

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

enter image description here

I believe the reason is that numbers convert df$age to a formattable numeric object and create NA , and df[is.na(df)] = '' converts df$age to a formattable character object:

 > 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" 

Any ideas for a solution?

Ultimately, I would also like to use this with filtered data.frame, where I use the code from Formatted Filtering Data Frames to ensure that the color bar remains the same when filtering .frame data:

 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) 

enter image description here

The problem does not seem to be related to this code.

+7
r
source share
1 answer

You can use the sprintf function to format numeric columns as strings with the desired number of decimal places. In the code below, sprintf converts NA to the string "NA" , which we then convert to an empty string.

 # 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) )) 

enter image description here

+1
source share

All Articles