Ask Your Question
1

How can we create summary tables by using nested tibbles?

asked 2021-08-04 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-06-22 19:00:00 +0000

qstack gravatar image

We can create summary tables by using nested tibbles in R by following these steps:

  1. Create a tibble with the data we want to summarize.
  2. Group the tibble by the variables we want to summarize by, using the group_by() function.
  3. Create a nested tibble by using the nest() function, which creates a new column with a tibble of the data for each group.
  4. Apply summary functions to the nested tibble with map() or purrr::map() functions, which calculates summaries for each group and returns a tibble of summary statistics.
  5. Unnest the summary tibble with the 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-08-04 11:00:00 +0000

Seen: 22 times

Last updated: Jun 22 '22