I am trying to create a data.frame where some cells have missing values. Instead of showing NAs, I wonder if there is any way to hide the NAs? What I want essentially is like an ANOVA table as shown below.
x = rnorm(40)
y = rep(1:2, each=20)
z = rep(c(1,2,1,2), each=10)
model1 = lm(x~y * z)
model2 = lm(x~y + z)
anova(model1, model2)
#Analysis of Variance Table
#Model 1: x ~ y * z
#Model 2: x ~ y + z
#Res.Df RSS Df Sum of Sq F Pr(>F)
#1 36 38.931
#2 37 39.248 -1 -0.31705 0.2932 0.5915
The output is above. If you try to access those blank cells, you will get NAs
anova(model1, model2)[1,4]
#[1] NA
Thanks in advance!!
print.anovaworks by using thena.printoption ofprint.default, like this:However, this only works for matrices, not for data frames.
For data frames, the suggestion of replacing the NA’s with “” is good, but loses the usual printing of digits; you can do so by using
format.data.framebefore replacement. Read theprint.data.framefunction for more details. Doing it this way, you can also replace with NA and then use thena.printoption, as above.See how the digits in the fourth column line up? Compare to this.