Based on the provided code and data, it appears that you want to create a function ttsummary that takes in a tibble data and a list of functions funcs. The function will apply each function in funcs to every column of data, summarize the results, and return a new tibble with the summarized values.
Here’s an updated version of your code with some additional explanations and comments:
# Define a function that takes in data and a list of functions
ttsummary <- function(data, funcs) {
# Create a temporary tibble to store the column names
st <- as_tibble(names(data))
# Loop through each function in funcs
for (i in 1:length(funcs)) {
# Apply the function to every column of data and summarize the results
tmp <- t(summarise_all(data, funcs[[i]]))[,1]
# Add the summarized values to the temporary tibble
st <- add_column(st, tmp, .name_repair = "unique")
}
# Rename the columns in the temporary tibble to include the function names
names(st) <- append("column", names(funcs))
# Return the final tibble with summarized values
return(st)
}
# Define a list of functions that can be applied to data
sf <- c(
mean = ~mean(., na.rm = TRUE),
num_zeros = ~sum(. == 0, na.rm =TRUE),
kurt = ~kurtosis(., na.rm = TRUE),
nans = ~sum(is.na(.))
)
# Select only numeric columns from the data
numerical_cols <- select_if(data, is.numeric)
# Apply the ttsummary function to the numerical columns and sf list
sumtable <- ttsummary(numerical_cols, sf)
This code defines a ttsummary function that takes in a tibble data and a list of functions funcs. It applies each function in funcs to every column of data, summarizes the results, and returns a new tibble with the summarized values.
The code also defines a list of functions sf that can be applied to data, including mean, number of zeros, kurtosis, and number of missing values.
Finally, it selects only numeric columns from the original data using the select_if function, applies the ttsummary function to these columns and the sf list, and stores the result in a new tibble called sumtable.
Last modified on 2024-11-13