Getting Table() to Return Zeroes in R

Getting table() to return zeroes in R

I haven't tested this, but I believe you want

table(factor(x, levels = 0:14))

Getting table() to return zeroes in R

I haven't tested this, but I believe you want

table(factor(x, levels = 0:14))

Include levels of zero count in result of table()

Convert your variable to a factor, and set the categories you wish to include in the result using levels. Values with a count of zero will then also appear in the result:

y <- c(0, 0, 1, 3, 4, 4)
table(factor(y, levels = 0:5))
# 0 1 2 3 4 5
# 2 1 0 1 2 0

table() in R needs to return zero if value is not present

If your data is a factor with appropriate levels, then you'll have no problem:

> x <- factor(letters[1:3])
> y <- factor(letters[1:3], levels = letters)

> table(x)
x
a b c
1 1 1

> table(y)
y
a b c d e f g h i j k l m n o p q r s t u v w x y z
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

> table(x)[["g"]]
Error in table(x)[["g"]] : subscript out of bounds

> table(y)[["g"]]
[1] 0

Just set the levels!

How to get the row numbers having zero values in a column by using which option?

It may be because the values are not exactly equal to 0. This can be checked by taking the difference with 0. By

y$V2[2] - 0

In such cases, an option is to round and check

which(round(y$V2) == 0)

Special zero value in gt tables

I think that gt::text_transform() can solve your immediate problem.

library(gt)
library(magrittr)
data <- data.frame(x=c(1.23,4.56,0,0,0,50,1.5))
table <- data %>%
gt() %>%
fmt_currency(x)

table

table %>%
text_transform(
locations = cells_body(
x,
rows = x==0
),
fn = function(x){
"-"
}
)

Image of gt table where the 0 is replaced with '-'

Multiple Columns

If you want to do it across multiple columns, you may want to also wrap it into a function and call against specific columns.

data <- data.frame(
x = c( 0, -0.230, 0, -0.445, 0),
y = c( -0.230, 0.0705, 0.460, -0.686, 0),
z = c( 0, 0, 0.07, 0.129, -0.68)
)

currency_dash <- function(gt_data, col_name) {
text_transform(
gt_data,
locations = cells_body(
columns = {{ col_name }},
rows = {{ col_name }} == 0
),
fn = function(x) {
"-"
}
)
}

data %>%
gt() %>%
fmt_currency(columns = everything()) %>%
currency_dash(x) %>%
currency_dash(y) %>%
currency_dash(z)

Image of multi-column table

General Transform

But you'd likely be better suited with just putting the logic into the text_transform().

data <- data.frame(
x = c( 0, -0.230, 0, -0.445, 0),
y = c( -0.230, 0.0705, 0.460, -0.686, 0),
z = c( 0, 0, 0.07, 0.129, -0.68)
)

table_currency <- data %>%
gt() %>%
fmt_currency(everything())

table_currency %>%
text_transform(
locations = cells_body(),
fn = function(x) ifelse(x == "$0.00", "-", x))
)

Table output is correct with the map_chr function

Showing cells with zero instances of a factor in a summary table instead of omitting them

We may use complete along with ungroup (without it we would get too many combinations):

df2 %>% group_by(var1, var2) %>% summarise(count = n()) %>% ungroup() %>%
complete(var1, var2, fill = list(count = 0))
# A tibble: 4 x 3
# var1 var2 count
# <fct> <fct> <dbl>
# 1 A C 3
# 2 A D 0
# 3 B C 7
# 4 B D 0

or complete and distinct:

df2 %>% group_by(var1, var2) %>% summarise(count = n()) %>%
complete(var1, var2, fill = list(count = 0)) %>% distinct()
# A tibble: 4 x 3
# var1 var2 count
# <fct> <fct> <dbl>
# 1 A C 3
# 2 A D 0
# 3 B C 7
# 4 B D 0


Related Topics



Leave a reply



Submit