Cross-column summary after `summary_rows` with gt - r

This is a toy example, so the numbers are meaningless, but how would I calculate the summary statistics for proportion in the summary_rows row of the table per group?
proportion is a row-wise calculation, so I can't sum/mean/sd/etc. For average proportion, for example, I want to calculate num[average] / items[average]. I can't figure out how to get a custom function to work for fns across groups.
exibble_a <-
exibble %>%
mutate(items = runif(8,10,20)) %>%
group_by(group) %>%
mutate(proportion = ifelse(is.na(items / num), 0, items / num)) %>%
ungroup() %>%
select(-c(fctr, date, time, datetime))
exibble_b =
exibble_a %>%
group_by(group) %>%
gt(rowname_col = "row", groupname_col = "group") %>%
fmt_missing(columns = everything()) %>%
fmt_percent(columns = proportion,
decimals = 2) %>%
summary_rows(groups = TRUE,
columns = c(num,items),
fns = list(
average = ~ mean(.,na.rm=TRUE),
total = ~ sum(.,na.rm=TRUE),
SD = ~ sd(.,na.rm=TRUE))) %>%
summary_rows(groups = TRUE,
columns = proportion,
fns = list(
average = ~ mean(exibble_a$items) / mean(exibble_a$num,
na.rm=TRUE)),
formatter = fmt_percent,
decimals = 2,
use_seps = TRUE)
exibble_b

Related

Change row group labels in gt table (with superscript/subscript and line breaks). Customising row group labels in R

I have the following data and table:
library(gt)
library(dplyr)
a <- rnorm(21, mean = 112, sd =12)
colour <- rep(c("Blue", "Red", "Green"), 7)
data <- data.frame(colour, a) %>%
group_by(colour) %>%
summarise(mean = mean(a), sd = sd(a), n = n()) %>%
mutate(grp = html("[H<sub>2</sub>O]")) %>%
rename(cat = colour)
b <- rnorm(21, mean = 60, sd =12)
day <- rep(c("2", "4", "6"), 7)
data2 <- data.frame(day, b) %>%
group_by(day) %>%
summarise(mean = mean(a), sd = sd(a), n = n()) %>%
mutate(grp = html("[H<sub>2</sub>O] Additition <br> (Days)")) %>%
rename(cat = day)
bind_rows(data, data2) %>%
group_by(grp) %>%
gt(rowname_col = "cat")
bind_rows(data, data2) %>%
group_by(grp) %>%
gt() %>%
tab_options(row_group.as_column = TRUE)
The row group labels appear literally as '[H<sub>2<\sub>O]', rather than [H2O] etc. It is likely that I am using HTML wrong and it needs to be used with another package/function. I have also tried using cols_label but doesn't recognise these as columns in the dataframe.
Is there also a way to have the row groups column vertically centered, rather than at the top where is currently is? How do you bold these row groups?
The html function won't work outside of a gt table, so you'll have to create the row groups using tab_row_group and add the html labels there.
data <- data.frame(colour, a) %>%
group_by(colour) %>%
summarise(mean = mean(a), sd = sd(a), n = n()) %>%
mutate(grp = "color") %>%
rename(cat = colour)
data2 <- data.frame(day, b) %>%
group_by(day) %>%
summarise(mean = mean(a), sd = sd(a), n = n()) %>%
mutate(grp = "day") %>%
rename(cat = day)
bind_rows(data, data2) %>%
gt() %>%
tab_row_group(
label = html("[H<sub>2</sub>O]"),
rows = grp == "color"
) %>%
tab_row_group(
label = html("[H<sub>2</sub>O] Additition <br> (Days)"),
rows = grp == "day"
) %>%
cols_hide(grp)

Order Data table based on Category in R

I have a data table that I created in R to compare the percentage between a population and sample. Here is the script I created for the table:
team_pop <- team_new %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(pop = n/sum(n)) %>%
arrange(desc(pop)) %>%
adorn_totals()
team_sample <- sample_final %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(sam = n/sum(n)) %>%
arrange(desc(sam)) %>%
adorn_totals()
datatable(
team_pop %>%
select(-n) %>%
left_join(team_sample %>%
select(degree,
sam),
by = "degree"),
rownames = FALSE,
colnames = c(
"Degree"= "degree",
"Population" = "pop",
"Sample" = "sam"),
options = list(info = FALSE,
paging = FALSE,
searching = FALSE)
) %>%
formatPercentage(2, digits = 0) %>%
formatPercentage(3, digits = 0)
Here is what the data looks like:
Degree
Population
Sample
Medium
45%
43%
Low
35%
37%
High
20%
20%
But this is how I want me data to be ordered in my data table:
Degree
Population
Sample
High
20%
20%
Medium
45%
43%
Low
35%
37%
Just wondering if someone could please help me order my categories based on the Degree column from High to Low. I have tried the R function "sort" using the formula below, but it still orders my data based on the highest number first:
team_pop <- team_new %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(pop = n/sum(n)) %>%
sort(degree, degreasing = FALSE) %>%
adorn_totals()
team_sample <- sample_final %>%
group_by(degree) %>%
count() %>%
ungroup() %>%
mutate(sam = n/sum(n)) %>%
sort(degree, degreasing = FALSE) %>%
adorn_totals()
datatable(
team_pop %>%
select(-n) %>%
left_join(team_sample %>%
select(degree,
sam),
by = "degree"),
rownames = FALSE,
colnames = c(
"Degree"= "degree",
"Population" = "pop",
"Sample" = "sam"),
options = list(info = FALSE,
paging = FALSE,
searching = FALSE)
) %>%
formatPercentage(2, digits = 0) %>%
formatPercentage(3, digits = 0)
Make your degree variable a factor and then arrange based on degree.
team_pop <- team_pop %>%
mutate(Degree=factor(Degree, levels = c("High", "Medium", "Low"))) %>%
arrange(Degree)

Skip "zero" level of dichotomous variables in expss tables

I want to create a summary table for some dichotomous variables using the expss package. Since the variables are dichotomous, one of the two levels would the sufficient to "show the picture".
I tried to use the function tab_net_cell, but was not able to get the right results. Here is some example code with BrCa (Breast cancer) with 1 or 0. I only want to show the number of patients with but not without breast cancer.
df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
df$group <- c(1,2,1,2,1,2,1,2,1,2)
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_net_cells("BrCa" = eq(1)) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")
The simplest way is to filter resulted table:
df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
df$group <- c(1,2,1,2,1,2,1,2,1,2)
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows") %>%
expss::where(grepl(1, row_labels))
Another way is to use mean and sum instead of cpct and cases:
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa*100) %>%
expss::tab_stat_mean(label = "%") %>%
expss::tab_stat_sum(label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")

Pretty tables with cumulative count / percentage and group totals using R "tables" package

I am trying to produce a formatted html table which has columns for frequency, cumulative frequency, column percentage, and cumulative column percentage. The table should also have the data subsetted by a grouping variable, and including a group total.
I can almost achieve this using a combination of dplyr and tidyr, but the output is a dataframe which doesn't look so pretty. I wonder if there is an easier way using the tables::tabulate command?
# Sample data
dat <- data.frame(
id = 1:100,
group = factor(sample(c("A", "B"), 100, replace = TRUE)),
sessions = factor(sample(1:10, 100, replace = TRUE))
)
# dplyr/tidyr solution
library(dplyr)
library(tidyr)
dat %>%
group_by(group, sessions) %>%
tally() %>%
spread(key = group, value = n) %>%
mutate(All = rowSums(.[-1])) %>%
gather(key = group, value = n, -sessions) %>%
group_by(group) %>%
mutate(
cum_n = cumsum(n),
p = round(n / sum(n)*100,1),
cum_p = round(cum_n / sum(n)*100,1),
) %>%
data.frame() %>%
reshape(timevar = "group", idvar = "sessions", direction = "wide")
# As far as I get using tables::tabulate
library(tables)
tabular(
Factor(sessions, "Sessions") ~
(Heading()*group + 1) *
(
(n = 1) +
# (cum_n = ??) +
Heading("%")*Percent(denom = "col")*Format(digits = 2)
# + Heading("cum_%")*??*Format(digits = 2)
),
data = dat
)
I would recommend using knitr::kable and kableExtra, amazing packages for producing tables. You can also set it up for multiple format outputs, for example using the same code to produce html and latex for pdf.
library(dplyr)
library(tidyr)
library(knitr)
library(kableExtra)
dat %>%
group_by(group, sessions) %>%
tally() %>%
spread(key = group, value = n) %>%
mutate(All = rowSums(.[-1])) %>%
gather(key = group, value = n, -sessions) %>%
group_by(group) %>%
mutate(
cum_n = cumsum(n),
p = round(n / sum(n)*100,1),
cum_p = round(cum_n / sum(n)*100,1),
) %>%
data.frame() %>%
reshape(timevar = "group", idvar = "sessions", direction = "wide") %>%
kable("html") %>%
kable_styling(bootstrap_options = c("striped", "hover"))

Moving mean as a function in dplyr

I'd like to create a function that can calculate the moving mean for a variable number of last observations and different variables. Take this as mock data:
df = expand.grid(site = factor(seq(10)),
year = 2000:2004,
day = 1:50)
df$temp = rpois(dim(df)[1], 5)
Calculating for 1 variable and a fixed number of last observations works. E.g. this calculates the average of the temperature of the last 5 days:
library(dplyr)
library(zoo)
df <- df %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(almost_avg = rollmean(x = temp, 5, align = "right", fill = NA)) %>%
mutate(avg = lag(almost_avg, 1))
So far so good. Now trying to functionalize fails.
avg_last_x <- function(dataframe, column, last_x) {
dataframe <- dataframe %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(almost_avg = rollmean(x = column, k = last_x, align = "right", fill = NA)) %>%
mutate(avg = lag(almost_avg, 1))
return(dataframe) }
avg_last_x(dataframe = df, column = "temp", last_x = 10)
I get this error:
Error in mutate_impl(.data, dots) : k <= n is not TRUE
I understand this is probably related to the evaluation mechanism in dplyr, but I don't get it fixed.
Thanks in advance for your help.
This should fix it.
library(lazyeval)
avg_last_x <- function(dataframe, column, last_x) {
dataframe %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate_(almost_avg = interp(~rollmean(x = c, k = last_x, align = "right",
fill = NA), c = as.name(column)),
avg = ~lag(almost_avg, 1))
}

Resources