打印数据框时不要打印NA

Tom*_*eif 6 r output-formatting na

因为data frames没有na.print 选择.是否有任何解决方法来抑制NA的显示?

样本数据框:

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
Run Code Online (Sandbox Code Playgroud)

结果是:

  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
Run Code Online (Sandbox Code Playgroud)

但我想表明:

  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1
3 c 1  1
4 d 1
Run Code Online (Sandbox Code Playgroud)

ags*_*udy 7

您可以替换缺失值""(此方法在print.distS3方法中使用)

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    
Run Code Online (Sandbox Code Playgroud)


Sim*_*lon 5

有,但你必须首先强制使用矩阵......

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 
Run Code Online (Sandbox Code Playgroud)

如果你愿意的话,将它鞭打成一个小功能......

nona <- function(x){
    print( as.matrix(x) , na.print = "" , quote = FALSE )
}
Run Code Online (Sandbox Code Playgroud)