2012-12-04 27 views
1

指定缺少值的打印我正在使用format来创建可读取的平坦表,其中ftable不足。一个警告是,在使用prop.table函数计算列频率时,NA值会打印为NA,这会导致混乱和可读性差。R以格式

如何修改下面的代码以使用空格或句点打印NANaN值?我考虑使用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 %)" 

回答

0

一个简单的方法是通过行和列解析(如果你的数据不是大):

no_row=nrow(o) 
no_col=ncol(o) 

for(rows in 1:no_row){ 
    for(cols in 1:no_col){ 
    o[rows,cols]<-sub(pattern = "NaN", replacement = "0", x = o[rows,cols]) 
    } 
} 

但当然,有更简单的方法来做到这一点。 :) 以上代码的输出是:

> o 
    y 
x 1    2    3   
    1 "17 ( 31 %)" "13 ( 29 %)" " 0 (0 %)" 
    2 "16 ( 29 %)" "21 ( 47 %)" " 0 (0 %)" 
    3 "22 ( 40 %)" "11 ( 24 %)" " 0 (0 %)" 

希望这将有助于!

1
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) 
p <- round(100*p,digits=0) 
p[is.na(p) ] <- " " 
o <- structure(
    paste(format(t), '(', format(p), '%)'), 
    dim=dim(t), 
    dimnames=dimnames(t) 
) 
o 
#------------------------- 
    y 
x 1   2   3   
    1 "17 (34 %)" "14 (28 %)" " 0 ( %)" 
    2 "15 (30 %)" "17 (34 %)" " 0 ( %)" 
    3 "18 (36 %)" "19 (38 %)" " 0 ( %)" 

用任何你想要的字符串替换空白(“”)。