Removing Zeros from Frequency Column

Hi all,

In the example presented below, I was interested in observing the frequency of Likert scale across the 5 variables, so I generated a table.

However, I wondered if there was a way to remove rows in which the frequency of events is zero?

Any help here would be appreciated.

dat <- data.frame(
  matrix(
    sample(1:3,5*12, replace=TRUE),12,5,
    dimnames=list(1:12,c("X1","X2","X3","X4","X5"))
  ),
  Sex=rep(c("Male", "Female")))

freq <- as.data.frame(table(dat))

There certainly is!

dat <- data.frame(
  matrix(
    sample(1:3,5*12, replace=TRUE),12,5,
    dimnames=list(1:12,c("X1","X2","X3","X4","X5"))
  ),
  Sex=rep(c("Male", "Female")))

(freq <- as.data.frame(table(dat)))
#>     X1 X2 X3 X4 X5    Sex Freq
#> 1    1  1  1  1  1 Female    0
#> 2    2  1  1  1  1 Female    0
#> 3    1  2  1  1  1 Female    0
#> 4    2  2  1  1  1 Female    0
#> 5    1  3  1  1  1 Female    0
#> 6    2  3  1  1  1 Female    0
#> 7    1  1  2  1  1 Female    0
#> 8    2  1  2  1  1 Female    0
#> 9    1  2  2  1  1 Female    0
#> 10   2  2  2  1  1 Female    0
#> 11   1  3  2  1  1 Female    0
#> 12   2  3  2  1  1 Female    0
#> 13   1  1  3  1  1 Female    0
#> 14   2  1  3  1  1 Female    0
#> 15   1  2  3  1  1 Female    0
#> 16   2  2  3  1  1 Female    0
#> 17   1  3  3  1  1 Female    0
#> 18   2  3  3  1  1 Female    0
#> 19   1  1  1  2  1 Female    0
#> 20   2  1  1  2  1 Female    0
#> 21   1  2  1  2  1 Female    0
#> 22   2  2  1  2  1 Female    0
#> 23   1  3  1  2  1 Female    0
#> 24   2  3  1  2  1 Female    0
#> 25   1  1  2  2  1 Female    0
#> 26   2  1  2  2  1 Female    0
#> 27   1  2  2  2  1 Female    0
#> 28   2  2  2  2  1 Female    0
#> 29   1  3  2  2  1 Female    0
#> 30   2  3  2  2  1 Female    0
#> 31   1  1  3  2  1 Female    0
#> 32   2  1  3  2  1 Female    0
#> 33   1  2  3  2  1 Female    0
#> 34   2  2  3  2  1 Female    0
#> 35   1  3  3  2  1 Female    0
#> 36   2  3  3  2  1 Female    0
#> 37   1  1  1  3  1 Female    0
#> 38   2  1  1  3  1 Female    0
#> 39   1  2  1  3  1 Female    0
#> 40   2  2  1  3  1 Female    0
#> 41   1  3  1  3  1 Female    0
#> 42   2  3  1  3  1 Female    0
#> 43   1  1  2  3  1 Female    0
#> 44   2  1  2  3  1 Female    0
#> 45   1  2  2  3  1 Female    0
#> 46   2  2  2  3  1 Female    0
#> 47   1  3  2  3  1 Female    0
#> 48   2  3  2  3  1 Female    0
#> 49   1  1  3  3  1 Female    0
#> 50   2  1  3  3  1 Female    0
#> 51   1  2  3  3  1 Female    0
#> 52   2  2  3  3  1 Female    0
#> 53   1  3  3  3  1 Female    0
#> 54   2  3  3  3  1 Female    0
#> 55   1  1  1  1  2 Female    0
#> 56   2  1  1  1  2 Female    0
#> 57   1  2  1  1  2 Female    0
#> 58   2  2  1  1  2 Female    0
#> 59   1  3  1  1  2 Female    0
#> 60   2  3  1  1  2 Female    0
#> 61   1  1  2  1  2 Female    0
#> 62   2  1  2  1  2 Female    0
#> 63   1  2  2  1  2 Female    0
#> 64   2  2  2  1  2 Female    0
#> 65   1  3  2  1  2 Female    0
#> 66   2  3  2  1  2 Female    0
#> 67   1  1  3  1  2 Female    0
#> 68   2  1  3  1  2 Female    0
#> 69   1  2  3  1  2 Female    0
#> 70   2  2  3  1  2 Female    0
#> 71   1  3  3  1  2 Female    0
#> 72   2  3  3  1  2 Female    1
#> 73   1  1  1  2  2 Female    0
#> 74   2  1  1  2  2 Female    1
#> 75   1  2  1  2  2 Female    0
#> 76   2  2  1  2  2 Female    0
#> 77   1  3  1  2  2 Female    1
#> 78   2  3  1  2  2 Female    0
#> 79   1  1  2  2  2 Female    0
#> 80   2  1  2  2  2 Female    0
#> 81   1  2  2  2  2 Female    0
#> 82   2  2  2  2  2 Female    0
#> 83   1  3  2  2  2 Female    0
#> 84   2  3  2  2  2 Female    0
#> 85   1  1  3  2  2 Female    0
#> 86   2  1  3  2  2 Female    0
#> 87   1  2  3  2  2 Female    0
#> 88   2  2  3  2  2 Female    0
#> 89   1  3  3  2  2 Female    0
#> 90   2  3  3  2  2 Female    0
#> 91   1  1  1  3  2 Female    0
#> 92   2  1  1  3  2 Female    0
#> 93   1  2  1  3  2 Female    0
#> 94   2  2  1  3  2 Female    0
#> 95   1  3  1  3  2 Female    0
#> 96   2  3  1  3  2 Female    0
#> 97   1  1  2  3  2 Female    0
#> 98   2  1  2  3  2 Female    0
#> 99   1  2  2  3  2 Female    0
#> 100  2  2  2  3  2 Female    0
#> 101  1  3  2  3  2 Female    0
#> 102  2  3  2  3  2 Female    0
#> 103  1  1  3  3  2 Female    0
#> 104  2  1  3  3  2 Female    1
#> 105  1  2  3  3  2 Female    0
#> 106  2  2  3  3  2 Female    0
#> 107  1  3  3  3  2 Female    0
#> 108  2  3  3  3  2 Female    0
#> 109  1  1  1  1  3 Female    0
#> 110  2  1  1  1  3 Female    0
#> 111  1  2  1  1  3 Female    0
#> 112  2  2  1  1  3 Female    0
#> 113  1  3  1  1  3 Female    0
#> 114  2  3  1  1  3 Female    0
#> 115  1  1  2  1  3 Female    0
#> 116  2  1  2  1  3 Female    0
#> 117  1  2  2  1  3 Female    0
#> 118  2  2  2  1  3 Female    0
#> 119  1  3  2  1  3 Female    0
#> 120  2  3  2  1  3 Female    0
#> 121  1  1  3  1  3 Female    0
#> 122  2  1  3  1  3 Female    0
#> 123  1  2  3  1  3 Female    0
#> 124  2  2  3  1  3 Female    0
#> 125  1  3  3  1  3 Female    0
#> 126  2  3  3  1  3 Female    0
#> 127  1  1  1  2  3 Female    0
#> 128  2  1  1  2  3 Female    0
#> 129  1  2  1  2  3 Female    0
#> 130  2  2  1  2  3 Female    0
#> 131  1  3  1  2  3 Female    0
#> 132  2  3  1  2  3 Female    0
#> 133  1  1  2  2  3 Female    0
#> 134  2  1  2  2  3 Female    0
#> 135  1  2  2  2  3 Female    0
#> 136  2  2  2  2  3 Female    0
#> 137  1  3  2  2  3 Female    0
#> 138  2  3  2  2  3 Female    0
#> 139  1  1  3  2  3 Female    1
#> 140  2  1  3  2  3 Female    0
#> 141  1  2  3  2  3 Female    0
#> 142  2  2  3  2  3 Female    0
#> 143  1  3  3  2  3 Female    0
#> 144  2  3  3  2  3 Female    0
#> 145  1  1  1  3  3 Female    1
#> 146  2  1  1  3  3 Female    0
#> 147  1  2  1  3  3 Female    0
#> 148  2  2  1  3  3 Female    0
#> 149  1  3  1  3  3 Female    0
#> 150  2  3  1  3  3 Female    0
#> 151  1  1  2  3  3 Female    0
#> 152  2  1  2  3  3 Female    0
#> 153  1  2  2  3  3 Female    0
#> 154  2  2  2  3  3 Female    0
#> 155  1  3  2  3  3 Female    0
#> 156  2  3  2  3  3 Female    0
#> 157  1  1  3  3  3 Female    0
#> 158  2  1  3  3  3 Female    0
#> 159  1  2  3  3  3 Female    0
#> 160  2  2  3  3  3 Female    0
#> 161  1  3  3  3  3 Female    0
#> 162  2  3  3  3  3 Female    0
#> 163  1  1  1  1  1   Male    0
#> 164  2  1  1  1  1   Male    0
#> 165  1  2  1  1  1   Male    0
#> 166  2  2  1  1  1   Male    1
#> 167  1  3  1  1  1   Male    0
#> 168  2  3  1  1  1   Male    0
#> 169  1  1  2  1  1   Male    0
#> 170  2  1  2  1  1   Male    0
#> 171  1  2  2  1  1   Male    0
#> 172  2  2  2  1  1   Male    0
#> 173  1  3  2  1  1   Male    0
#> 174  2  3  2  1  1   Male    0
#> 175  1  1  3  1  1   Male    0
#> 176  2  1  3  1  1   Male    0
#> 177  1  2  3  1  1   Male    0
#> 178  2  2  3  1  1   Male    0
#> 179  1  3  3  1  1   Male    0
#> 180  2  3  3  1  1   Male    0
#> 181  1  1  1  2  1   Male    0
#> 182  2  1  1  2  1   Male    0
#> 183  1  2  1  2  1   Male    0
#> 184  2  2  1  2  1   Male    0
#> 185  1  3  1  2  1   Male    0
#> 186  2  3  1  2  1   Male    0
#> 187  1  1  2  2  1   Male    0
#> 188  2  1  2  2  1   Male    0
#> 189  1  2  2  2  1   Male    0
#> 190  2  2  2  2  1   Male    0
#> 191  1  3  2  2  1   Male    0
#> 192  2  3  2  2  1   Male    0
#> 193  1  1  3  2  1   Male    0
#> 194  2  1  3  2  1   Male    0
#> 195  1  2  3  2  1   Male    0
#> 196  2  2  3  2  1   Male    0
#> 197  1  3  3  2  1   Male    0
#> 198  2  3  3  2  1   Male    0
#> 199  1  1  1  3  1   Male    0
#> 200  2  1  1  3  1   Male    0
#> 201  1  2  1  3  1   Male    0
#> 202  2  2  1  3  1   Male    0
#> 203  1  3  1  3  1   Male    0
#> 204  2  3  1  3  1   Male    0
#> 205  1  1  2  3  1   Male    0
#> 206  2  1  2  3  1   Male    1
#> 207  1  2  2  3  1   Male    0
#> 208  2  2  2  3  1   Male    0
#> 209  1  3  2  3  1   Male    0
#> 210  2  3  2  3  1   Male    0
#> 211  1  1  3  3  1   Male    0
#> 212  2  1  3  3  1   Male    0
#> 213  1  2  3  3  1   Male    0
#> 214  2  2  3  3  1   Male    0
#> 215  1  3  3  3  1   Male    0
#> 216  2  3  3  3  1   Male    0
#> 217  1  1  1  1  2   Male    0
#> 218  2  1  1  1  2   Male    0
#> 219  1  2  1  1  2   Male    0
#> 220  2  2  1  1  2   Male    0
#> 221  1  3  1  1  2   Male    0
#> 222  2  3  1  1  2   Male    0
#> 223  1  1  2  1  2   Male    0
#> 224  2  1  2  1  2   Male    0
#> 225  1  2  2  1  2   Male    0
#> 226  2  2  2  1  2   Male    0
#> 227  1  3  2  1  2   Male    0
#> 228  2  3  2  1  2   Male    0
#> 229  1  1  3  1  2   Male    0
#> 230  2  1  3  1  2   Male    0
#> 231  1  2  3  1  2   Male    0
#> 232  2  2  3  1  2   Male    0
#> 233  1  3  3  1  2   Male    0
#> 234  2  3  3  1  2   Male    1
#> 235  1  1  1  2  2   Male    0
#> 236  2  1  1  2  2   Male    0
#> 237  1  2  1  2  2   Male    0
#> 238  2  2  1  2  2   Male    0
#> 239  1  3  1  2  2   Male    0
#> 240  2  3  1  2  2   Male    0
#> 241  1  1  2  2  2   Male    0
#> 242  2  1  2  2  2   Male    0
#> 243  1  2  2  2  2   Male    0
#> 244  2  2  2  2  2   Male    0
#> 245  1  3  2  2  2   Male    0
#> 246  2  3  2  2  2   Male    0
#> 247  1  1  3  2  2   Male    0
#> 248  2  1  3  2  2   Male    0
#> 249  1  2  3  2  2   Male    0
#> 250  2  2  3  2  2   Male    0
#> 251  1  3  3  2  2   Male    0
#> 252  2  3  3  2  2   Male    0
#> 253  1  1  1  3  2   Male    0
#> 254  2  1  1  3  2   Male    0
#> 255  1  2  1  3  2   Male    0
#> 256  2  2  1  3  2   Male    0
#> 257  1  3  1  3  2   Male    0
#> 258  2  3  1  3  2   Male    0
#> 259  1  1  2  3  2   Male    0
#> 260  2  1  2  3  2   Male    0
#> 261  1  2  2  3  2   Male    0
#> 262  2  2  2  3  2   Male    0
#> 263  1  3  2  3  2   Male    0
#> 264  2  3  2  3  2   Male    0
#> 265  1  1  3  3  2   Male    0
#> 266  2  1  3  3  2   Male    0
#> 267  1  2  3  3  2   Male    0
#> 268  2  2  3  3  2   Male    0
#> 269  1  3  3  3  2   Male    0
#> 270  2  3  3  3  2   Male    0
#> 271  1  1  1  1  3   Male    0
#> 272  2  1  1  1  3   Male    0
#> 273  1  2  1  1  3   Male    0
#> 274  2  2  1  1  3   Male    0
#> 275  1  3  1  1  3   Male    0
#> 276  2  3  1  1  3   Male    0
#> 277  1  1  2  1  3   Male    0
#> 278  2  1  2  1  3   Male    0
#> 279  1  2  2  1  3   Male    0
#> 280  2  2  2  1  3   Male    0
#> 281  1  3  2  1  3   Male    1
#> 282  2  3  2  1  3   Male    0
#> 283  1  1  3  1  3   Male    0
#> 284  2  1  3  1  3   Male    0
#> 285  1  2  3  1  3   Male    0
#> 286  2  2  3  1  3   Male    0
#> 287  1  3  3  1  3   Male    0
#> 288  2  3  3  1  3   Male    0
#> 289  1  1  1  2  3   Male    0
#> 290  2  1  1  2  3   Male    0
#> 291  1  2  1  2  3   Male    0
#> 292  2  2  1  2  3   Male    0
#> 293  1  3  1  2  3   Male    0
#> 294  2  3  1  2  3   Male    1
#> 295  1  1  2  2  3   Male    0
#> 296  2  1  2  2  3   Male    0
#> 297  1  2  2  2  3   Male    1
#> 298  2  2  2  2  3   Male    0
#> 299  1  3  2  2  3   Male    0
#> 300  2  3  2  2  3   Male    0
#> 301  1  1  3  2  3   Male    0
#> 302  2  1  3  2  3   Male    0
#> 303  1  2  3  2  3   Male    0
#> 304  2  2  3  2  3   Male    0
#> 305  1  3  3  2  3   Male    0
#> 306  2  3  3  2  3   Male    0
#> 307  1  1  1  3  3   Male    0
#> 308  2  1  1  3  3   Male    0
#> 309  1  2  1  3  3   Male    0
#> 310  2  2  1  3  3   Male    0
#> 311  1  3  1  3  3   Male    0
#> 312  2  3  1  3  3   Male    0
#> 313  1  1  2  3  3   Male    0
#> 314  2  1  2  3  3   Male    0
#> 315  1  2  2  3  3   Male    0
#> 316  2  2  2  3  3   Male    0
#> 317  1  3  2  3  3   Male    0
#> 318  2  3  2  3  3   Male    0
#> 319  1  1  3  3  3   Male    0
#> 320  2  1  3  3  3   Male    0
#> 321  1  2  3  3  3   Male    0
#> 322  2  2  3  3  3   Male    0
#> 323  1  3  3  3  3   Male    0
#> 324  2  3  3  3  3   Male    0

freq[freq$Freq > 0,]
#>     X1 X2 X3 X4 X5    Sex Freq
#> 72   2  3  3  1  2 Female    1
#> 74   2  1  1  2  2 Female    1
#> 77   1  3  1  2  2 Female    1
#> 104  2  1  3  3  2 Female    1
#> 139  1  1  3  2  3 Female    1
#> 145  1  1  1  3  3 Female    1
#> 166  2  2  1  1  1   Male    1
#> 206  2  1  2  3  1   Male    1
#> 234  2  3  3  1  2   Male    1
#> 281  1  3  2  1  3   Male    1
#> 294  2  3  1  2  3   Male    1
#> 297  1  2  2  2  3   Male    1

# alternatively...

dplyr::count(dat, X1, X2, X3, X4, X5, Sex)
#>    X1 X2 X3 X4 X5    Sex n
#> 1   1  1  1  3  3 Female 1
#> 2   1  1  3  2  3 Female 1
#> 3   1  2  2  2  3   Male 1
#> 4   1  3  1  2  2 Female 1
#> 5   1  3  2  1  3   Male 1
#> 6   2  1  1  2  2 Female 1
#> 7   2  1  2  3  1   Male 1
#> 8   2  1  3  3  2 Female 1
#> 9   2  2  1  1  1   Male 1
#> 10  2  3  1  2  3   Male 1
#> 11  2  3  3  1  2 Female 1
#> 12  2  3  3  1  2   Male 1

Created on 2022-08-29 by the reprex package (v2.0.1)

Hi @JackDavison. My apologies. But the zero frequencies still appear on the RHS? I would like to display only non-zero frequencies if possible?

Hi @AC3112, did you scroll further down?

freq[freq$Freq > 0,]
#>     X1 X2 X3 X4 X5    Sex Freq
#> 72   2  3  3  1  2 Female    1
#> 74   2  1  1  2  2 Female    1
#> 77   1  3  1  2  2 Female    1
#> 104  2  1  3  3  2 Female    1
#> 139  1  1  3  2  3 Female    1
#> 145  1  1  1  3  3 Female    1
#> 166  2  2  1  1  1   Male    1
#> 206  2  1  2  3  1   Male    1
#> 234  2  3  3  1  2   Male    1
#> 281  1  3  2  1  3   Male    1
#> 294  2  3  1  2  3   Male    1
#> 297  1  2  2  2  3   Male    1

# alternatively...

dplyr::count(dat, X1, X2, X3, X4, X5, Sex)
#>    X1 X2 X3 X4 X5    Sex n
#> 1   1  1  1  3  3 Female 1
#> 2   1  1  3  2  3 Female 1
#> 3   1  2  2  2  3   Male 1
#> 4   1  3  1  2  2 Female 1
#> 5   1  3  2  1  3   Male 1
#> 6   2  1  1  2  2 Female 1
#> 7   2  1  2  3  1   Male 1
#> 8   2  1  3  3  2 Female 1
#> 9   2  2  1  1  1   Male 1
#> 10  2  3  1  2  3   Male 1
#> 11  2  3  3  1  2 Female 1
#> 12  2  3  3  1  2   Male 1
1 Like

Hi @JackDavison.

My apologies. Silly oversight. Really appreciate your help and kind response.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.