Question

For data frames there is no na.print option. Is there any workaround to suppress display of NAs?

Sample 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:

  x a  b  c  d
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:

  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1
3 c 1  1
4 d 1
Was it helpful?

Solution

You can replace missing values by "" (this technique is used in print.dist S3 method)

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

OTHER TIPS

There is, but you have to coerce to a matrix first...

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

Whip it into a little function if you like...

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

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   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top