Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用r函数wtable在加权表中显示零?_R_Frequency_Weighted_Prop - Fatal编程技术网

如何使用r函数wtable在加权表中显示零?

如何使用r函数wtable在加权表中显示零?,r,frequency,weighted,prop,R,Frequency,Weighted,Prop,我很欣赏GDA工具包中的wtable功能,例如 GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec, stat='freq') 3 4 5 Sum 4 20,0 156,9 33,6 210,5 6 39,7 70,7 15,5 125,8 8 205,7 29,1 Sum 265,4 78,2 但是对于cyl==8和ge

我很欣赏GDA工具包中的wtable功能,例如

GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec, stat='freq')
        3     4     5   Sum
4    20,0 156,9  33,6 210,5
6    39,7  70,7  15,5 125,8
8   205,7        29,1      
Sum 265,4        78,2  
但是对于cyl==8和gear==4组合,这将显示空白而不是零。 因此,我无法使用道具选项:

GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec, stat='prop')
    3 4 5 Sum
4            
6            
8            
Sum   
也不适用于道具台:

prop.table(GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec))
    3 4 5 Sum
4            
6            
8            
Sum  
只有不带空格的行和/或列的rprop和cprob有效:

GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec, stat='rprop')
       3    4    5 Sum
4    9,5 74,5 16,0 100
6   31,5 56,2 12,3 100
8     NA   NA   NA  NA
Sum   NA   NA   NA  NA
GDAtools::wtable(mtcars$cyl,mtcars$gear,weights = mtcars$qsec, stat='cprop')
        3  4     5 Sum
4     7,5 NA  43,0  NA
6    14,9 NA  19,8  NA
8    77,5 NA  37,2  NA
Sum 100,0 NA 100,0  NA

有没有办法在wtable中强制为零而不是空白?

空白值实际上是以不同方式显示的
NA
值。您可以使用
is.na
捕获它们

tab <- GDAtools::wtable(mtcars$cyl,mtcars$gear, weights=mtcars$qsec, stat='freq')
tab[is.na(tab)] <- 0
tab

#        3     4     5   Sum
#4    20.0 156.9  33.6 210.5
#6    39.7  70.7  15.5 125.8
#8   205.7   0.0  29.1   0.0
#Sum 265.4   0.0  78.2   0.0
选项卡