对于data frames
,没有na.print
option。是否有任何解决方法来抑制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
答案 0 :(得分:7)
您可以按""
替换缺失值(此方法用于print.dist
S3方法)
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
答案 1 :(得分: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
如果你喜欢的话,将它鞭打成一个小功能......
nona <- function(x){
print( as.matrix(x) , na.print = "" , quote = FALSE )
}
答案 2 :(得分: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