We can create summary tables by using nested tibbles in R by following these steps:
group_by()
function.nest()
function, which creates a new column with a tibble of the data for each group.map()
or purrr::map()
functions, which calculates summaries for each group and returns a tibble of summary statistics.unnest()
function to flatten it and combine the summary statistics back into a single tibble.Here's an example:
library(tidyverse) # create a tibble with data data <- tribble( ~year, ~month, ~sales, 2020, "Jan", 100, 2020, "Feb", 200, 2020, "Mar", 300, 2021, "Jan", 400, 2021, "Feb", 500, 2021, "Mar", 600 ) # group by year and create a nested tibble nested_data <- data %>% group_by(year) %>% nest() # apply a summary function to calculate mean sales for each year summary_table <- nested_data %>% mutate(mean_sales = map(data, ~mean(.x$sales))) %>% select(year, mean_sales) %>% unnest() # print summary table summary_table
This code creates a summary table of mean sales for each year, which is calculated by nesting the data by year, using the map()
function to calculate mean sales for each group, and unnesting the resulting summary tibble.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-08-04 11:00:00 +0000
Seen: 22 times
Last updated: Jun 22 '22
How can one obtain live data from interactive broker through API?
What is the method to implement pagination for Firestore data in Flutter using ListView?
What is the reason for the submitted Django form's value being null in the database?
What is the process of moving information from one tab to another in Excel by transposing it?
What does "coxphw undefined columns selected" mean?
Is it not possible to change the data type of an array from 'O' to 'float64'?
How to perform a historical backfill from GA4 into BigQuery?
How can Redux be integrated with Ag-Grid's server-side row model in React?