6

因为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

结果是:

  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

但我想展示:

  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1
3 c 1  1
4 d 1
4

3 回答 3

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    
于 2013-10-22T11:24:01.897 回答
6

有,但您必须先强制​​转换为矩阵...

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 

如果你愿意,可以把它变成一个小功能......

nona <- function(x){
    print( as.matrix(x) , na.print = "" , quote = FALSE )
}
于 2013-10-22T11:29:03.647 回答
3

您可以使用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   
于 2013-10-22T11:23:04.643 回答