Do not print NA when printing a data frame

There is no na.print option for data frames . Is there any workaround to suppress display of HC?

Example data frame:

 df <- data.frame( x=c("a","b","c","d"), a=c(1,1,1,1), b=c(1,1,1,NA), c=c(1,1,NA,NA), d=c(1,NA,NA,NA)) df 

Results in:

  xabcd 1 a 1 1 1 1 2 b 1 1 1 NA 3 c 1 1 NA NA 4 d 1 NA NA NA 

But I would like to show:

  xabcd 1 a 1 1 1 1 2 b 1 1 1 3 c 1 1 4 d 1 
+6
source share
3 answers

You can replace the missing values ​​with "" (this method is used in the print.dist S3 method)

 cf <- format(dat) ## use format to set other options like digits, justify , ... cf[is.na(dat)] <- "" cf xabcd 1 a 1 1 1 1 2 b 1 1 1 3 c 1 1 4 d 1 
+7
source

Yes, but first you have to force the matrix ...

 print( as.matrix(df) , na.print = "" , quote = FALSE ) xabcd [1,] a 1 1 1 1 [2,] b 1 1 1 [3,] c 1 1 [4,] d 1 

Paste it into a small function if you want ...

 nona <- function(x){ print( as.matrix(x) , na.print = "" , quote = FALSE ) } 
+5
source

You can use write.table :

 write.table(dfr,sep="\t",col.names=NA,na="") "" "x" "a" "b" "c" "d" "1" "a" 1 1 1 1 "2" "b" 1 1 1 "3" "c" 1 1 "4" "d" 1 
+3
source

Source: https://habr.com/ru/post/956474/