指定缺少值的打印我正在使用format
来创建可读取的平坦表,其中ftable
不足。一个警告是,在使用prop.table
函数计算列频率时,NA
值会打印为NA
,这会导致混乱和可读性差。R以格式
如何修改下面的代码以使用空格或句点打印NA
或NaN
值?我考虑使用sub
函数,但是我相信如果列名包含这些字符值,那么它很麻烦并且容易出错。
x <- sample(c(1, 2, 3), 100, replace=TRUE)
y <- sample(factor(c(1, 2), levels=1:3), 100, replace=TRUE)
t <- table(x,y)
p <- prop.table(t, margin=2)
o <- structure(
paste(format(t), '(', format(round(100*p)), '%)'),
dim=dim(t),
dimnames=dimnames(t)
)
这是给定输出例如:
> o
y
x 1 2 3
1 "20 ( 38 %)" "21 ( 44 %)" " 0 (NaN %)"
2 "20 ( 38 %)" "16 ( 33 %)" " 0 (NaN %)"
3 "12 ( 23 %)" "11 ( 23 %)" " 0 (NaN %)"